Hi, Question about gregorian date... I initialize a GregorianCalendar to the 4th of January of 2005 like this: Calendar newCal = new GregorianCalendar(2005,0,4); System.out.println("Week is =" + newCal.get(Calendar.WEEK_OF_YEAR)); it prints out Week is =2 The problem is that the 4th of January of 2005 actually is week 1. What am I doing wrong ??? Thanks in advance ... Per
Actually, it is week 2. If you take a look at the javadoc for WEEK_OF_YEAR, it states that this value is dependant on getFirstDayOfWeek() and getMinimalDaysInFirstWeek(). Since 4 Jan 2005 is a Tuesday, getFirstDayOfWeek() is not figuring into this (unless you are in a locale where the first day of the week is Wednesday or some such...) getMinimalDaysInFirstWeek() will return one (1), so (again, depending on locale) the first week of 2005 is Saturday, January 1 2005 through Saturday, January 1, 2005. Sunday, 2 January 2005 through Saturday, 8 January 2005 are the second week. Hope that this helps.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I'd also like to point out that 31 December 2004 is also the first week of the year, as Sunday, 26 December 2004 through Friday, 31 December 2004 are "glommed" onto the first week of 2005. Again, this is all in the JavaDoc. [ May 07, 2003: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Originally posted by Don Liu: what is the definition of week number? does a week start from Sunday or Saturday?
It depends on the calendar/locale. use .getFirstDayOfWeek() to find out. In Western Civilization (TM), it is ususlly either Sunday (in the States) or Monday (in France, etc.)
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.