Java Calendar.WEEK_OF_YEAR返回该月的一周

时间:2016-01-04 10:22:36

标签: java date calendar

/* package whatever; // don't place package name! */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone {

    public static void main (String[] args) throws java.lang.Exception {
        try {
            // your code goes here
            SimpleDateFormat sdf= new SimpleDateFormat("dd-mm-yyyy");
            Calendar cal= Calendar.getInstance();
            String s = "12-12-2014";
            Date dte=sdf.parse(s);
            cal.setTime(dte);

            System.out.println( cal.get(Calendar.WEEK_OF_YEAR)+"");
        } catch (Exception e ) {}
    }
}

输出 3

我想从特定日期获得一年中的一周。我提到了java文档,但WEEK_OF_YEAR应该在50中。

1 个答案:

答案 0 :(得分:7)

您的SimpleDateFormat错误,请参阅JavaDoc

  

信|日期或时间|组件|演示文稿|实例

     

m |分钟|号码| 30

应该是:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");