• Post Reply Bookmark Topic Watch Topic
  • New Topic

GregorianCalendar  RSS feed

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a GregorianCalendar and using the set(int,int,int,int,int,int) method to set its values. As an example I use set(2004,0,1,0,0,0,0) for jan 1 2004. I then call method get(Calendar.DAY_OF_WEEK) and it returns 7. My understanding is that DAY_OF_WEEK is 0 based so 7 should be invalid. Moreover, I would expect it can calculate the field since it has the info it needs, year, month and day in month. Also, since Jan 1, 2004 was a thursday, being 0 based can't be it. I assume that it has not computed the DAY_OF_WEEK field. Since the methods that look like they could force this are protected, how do I get DAY_OF_WEEK?

ms
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I then call method get(Calendar.DAY_OF_WEEK) and it returns 7.

Are you sure? The following code prints "5" for me:

Note that I had to remove the last argument from your set() call, since there is no set() call in Calendar with that many arguemnts.

My understanding is that DAY_OF_WEEK is 0 based so 7 should be invalid.

No, DAY_OF_WEEK starts with 1 (SUNDAY) and ends at 7 (SATURDAY). A fact which is cleverly hidden by the people who documented the Calendar class. But from the API for DAY_OF_WEEK, note the link under "See also" labeled Constant Field Values. This gives you a list of the numeric values of the various Calendar constants, and we see that SUNDAY is 1, and SATURDAY is 7. And THURSDAY is 5, which corresponds to the 5 printed when I ran the code above.
[ January 03, 2005: Message edited by: Jim Yingst ]
 
Arjun Shastry
Ranch Hand
Posts: 1906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to change the first day of the week by calling a method:
calendar.setFirstDayOFWeek(Calendar.MONDAY);
but this doesn't seem to work.Descriptin says:Sunday is first day in US,Monday is first day in France etc.
In the above example,answer is 5.bcos its taking the values from default locale.I also tried using another locale,in constructor of GregorianCalendar but still giving the same result as 5.
Any idea?
[ January 04, 2005: Message edited by: Arjun Shastry ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Descriptin says:Sunday is first day in US,Monday is first day in France etc.

You're looking at the API for getFirstDayOfWeek(). But you're not using getFirstDayOfWeek() - you're using get(Calendar.DAY_OF_WEEK). Which in this case is Calendar.THURSDAY, which is 5. Period. Always. Even in France. Note that if you'd asked for get(Calendar.WEEK_IN_MONTH) or get(Calendar.WEEK_IN_YEAR), those do depend on getFirstDayOfWeek(). But get(Calendar.DAY_OF_WEEK) does not depend on locale. At all.
 
Arjun Shastry
Ranch Hand
Posts: 1906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explanation. .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!