Other | Other |
---|---|
1.1.8 1.1.8Fixed | 1.2.0Fixed |
Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
Name: mf23781 Date: 01/12/98 Calendar Week Numbers sometimes have value 0(zero) or 54. While testing for Year2000 problems, the Week Number value from aCalendar.get(Calendar.WEEK_OF_YEAR) was checked and found to have values 0 or 54 for certain Calendar/date values. The Java API specification makes no mention of possible values but the values of zero and 54 are confusing to the programmer/API user. The week number 54s appear to have just one day in them, and I think the week zeros ought to be week 52/53 from the previous year. The ISO standard 8601 suggests that week numbers should be in the range 1-53. There is a summary of the standard at the following internet address: http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html The results vary according to the Calendar setting of the FirstDayOfWeek and MinimalDaysInFirstWeek. The program attached below attempts to cycle through the variations and display the week number for various calendar/date values around the end/start of each year. Here is a sample of the output with values of 0 and 54: Test11 indicates that the FirstDayOfWeek and MinimalDaysInWeek are both set to 1 and so on. Test11 - 31 Dec 2000 54 Test11 - 31 Dec 2028 54 Test21 - 31 Dec 2012 54 Test12 - 1 Jan 2000 0 Test12 - 1 Jan 2005 0 Test12 - 1 Jan 2011 0 Test12 - 1 Jan 2022 0 Test12 - 1 Jan 2028 0 Test12 - 1 Jan 2033 0 Test13 - 1 Jan 1999 0 Test13 - 2 Jan 1999 0 Test13 - 1 Jan 2000 0 Test13 - 1 Jan 2005 0 Test13 - 1 Jan 2010 0 Test13 - 2 Jan 2010 0 Test13 - 1 Jan 2011 0 Test13 - 1 Jan 2016 0 Test13 - 2 Jan 2016 0 Test13 - 1 Jan 2021 0 Test13 - 2 Jan 2021 0 Test13 - 1 Jan 2022 0 Test13 - 1 Jan 2027 0 Test13 - 2 Jan 2027 0 Test13 - 1 Jan 2028 0 Test13 - 1 Jan 2033 0 Test13 - 1 Jan 2038 0 Test13 - 2 Jan 2038 0 Test14 - 1 Jan 1998 0 Test14 - 2 Jan 1998 0 Test14 - 3 Jan 1998 0 Test14 - 1 Jan 1999 0 Test14 - 2 Jan 1999 0 Test14 - 1 Jan 2000 0 Sample test program follows: import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Date; import java.text.SimpleDateFormat; public class TestWeekNo { private static SimpleDateFormat sdf = new SimpleDateFormat(); public static void main(String args[]) { int numYears=40, startYear=1997, numDays=15; String output, testDesc; GregorianCalendar testCal = (GregorianCalendar)Calendar.getInstance(); for (int firstDay=1; firstDay<=2; firstDay++) { for (int minDays=1; minDays<=7; minDays++) { testCal.setMinimalDaysInFirstWeek(minDays); testCal.setFirstDayOfWeek(firstDay); System.out.println(); testDesc = ("Test" +String.valueOf(firstDay) +String.valueOf(minDays)); System.out.println(testDesc + " => 1st day of week=" +String.valueOf(firstDay) +", minimum days in first week=" +String.valueOf(minDays)); for (int j=startYear; j<=startYear+numYears; j++) { testCal.set(j,11,25); for(int i=0; i<numDays; i++) { testCal.add(Calendar.DATE,1); output = testDesc + " - " + checkCalendar(testCal); System.out.println(output); } } } } } private static String checkCalendar(GregorianCalendar date) { String output,calWOY; int actWOY = date.get(Calendar.WEEK_OF_YEAR); Date d = date.getTime(); calWOY = String.valueOf(actWOY); sdf.setCalendar(date); sdf.applyPattern("d MMM yyyy"); output = sdf.format(d) + "\t"; output = output + "\t" + calWOY; return output; } } ======================================================================
|