Problem with Java Calendar, JDK 1.6.0.22

I have a problem getting the week of the year. JDK version 1.6.0.22 is installed on my machine, 1.6.0.21 on another machine. And both machines return different results:

(1.6.0.22) Week: 1
(1.6.0.21) Week: 52

For this code:

      try {
         Calendar current = new GregorianCalendar();
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
         Date d = df.parse("2010-12-28 19:04:38 GMT");
         current.setTime(d);
         int currentWeek = current.get(Calendar.WEEK_OF_YEAR);
         System.out.println("week is: "currentWeek);
      } catch (ParseException e) {
        e.printStackTrace();
      }

Why does JDK 1.6.0.22 give the wrong result?

+4
source share
2 answers

This excerpt from the API documentation explains why this difference can occur:

, WEEK_OF_YEAR 1 53. 1 - getFirstDayOfWeek(), getMinimalDaysInFirstWeek() .. , getMinimalDaysInFirstWeek(), getFirstDayOfWeek() 1 .

Calendar:

firstDayOfWeek, minimumDaysInFirstWeek .

, , !. -, 1 ​​ . :

    Calendar cal = new GregorianCalendar();
    System.out.println(Locale.getDefault());
    System.out.println(cal.getMinimalDaysInFirstWeek());
    System.out.println(cal.getFirstDayOfWeek());

, , . "1" - .

+9

/ JDK - JODA Time.

+1

All Articles