• Post Reply Bookmark Topic Watch Topic
  • New Topic

About GregorianCalendar  RSS feed

 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everybody. I have a question abou the GregorianCalendar and the DateFormatSymbol.

I understand that Calendar.SUNDAY has value 1, Calendar.MONDAY has value 2 but why does the getFirstDayOfWeek() returns 2 as if the first day of week is Calendar.MONDAY?

Also



In this month, starting day is Thuesday, thus the value of weekday is 3. So how come weekdayNames doesnt start with 0 where 0 would be sunday? Instead, it starts with 3, wich in monday, then adds 1 everytime it goes through the loop beacuse of add(). When it comes to 7, next one is 1 and ends with 2 wich is the same as firstDayOfWeek.

Another thing,



sunday has value 2012 which is, geniously deducted, this year. On the other hand, Calendar.MONDAY is 2. All of the code is from Core Java I.
Thank you!
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Skrlec wrote:I understand that Calendar.SUNDAY has value 1, Calendar.MONDAY has value 2 but why does the getFirstDayOfWeek() returns 2 as if the first day of week is Calendar.MONDAY?

Because that's probably the case. There are two popular settings for the first day of the week - Sunday or Monday. Apparently Calendar uses Monday. Fortunately, you can change that if you want to, with Calendar.setFirstDayOfWeek.

In this month, starting day is Thuesday, thus the value of weekday is 3. So how come weekdayNames doesnt start with 0 where 0 would be sunday? Instead, it starts with 3, wich in monday, then adds 1 everytime it goes through the loop beacuse of add(). When it comes to 7, next one is 1 and ends with 2 wich is the same as firstDayOfWeek.

You're mistaken. weekdayNames uses Calendar.SUNDAY through Calendar.SATURDAY for its indexes. It doesn't start with 3 at all (and 3 is Tuesday, not Monday). It starts at 0 like any other array, although that value is undefined and meaningless. Like I said, use Calendar.SUNDAY through Calendar.SATURDAY to access its values. Fortunately, d.get(Calendar.DAY_OF_WEEK) returns one of these values.

Another thing,



sunday has value 2012 which is, geniously deducted, this year. On the other hand, Calendar.MONDAY is 2. All of the code is from Core Java I.

You're calling the method incorrectly. The argument should be one of the calendar fields. You're using Calendar.SUNDAY, but that's just an int. It has the same value as Calendar.YEAR, which is 1. That means that inst.get(Calendar.SUNDAY) is the same as inst.get(Calendar.YEAR) which returns the year - 2012.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're mistaken. weekdayNames uses Calendar.SUNDAY through Calendar.SATURDAY for its indexes. It doesn't start with 3 at all (and 3 is Tuesday, not Monday). It starts at 0 like any other array, although that value is undefined and meaningless. Like I said, use Calendar.SUNDAY through Calendar.SATURDAY to access its values. Fortunately, d.get(Calendar.DAY_OF_WEEK) returns one of these values.


I gave to few information, im sorry. This month is may, thus weekday variable is set to 3 beacuse starting day is thuesday. So, the weekdayNames[weekday] is weekdayNames[3]. Thank you for the getFirstDayOfWeek information. I thought, since I'm using US locale where first day of the week is sunday, thus, the method should return 1.

Thank you for the rest of the answers.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Skrlec wrote:Thank you for the rest of the answers.

It should be pointed out that Calendar was designed before enums existed (and I'm still not sure why they haven't retrofitted them or created a new class that makes use of them), and the way its constants are defined shouldn't be repeated. For one thing, they are mostly ints, which allows the misuse that you exampled in your first post; for another, there is no way to distinguish between an attribute type (eg, Calendar.DAY_OF_WEEK) and a constant (Calendar.SUNDAY).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!