• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar And Date class Doubt

 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I took this example from Chapter 6 K&B. Here is the code



When i call it, it shows the date is 09 September but when i try to retrieve the month in line one, it returns 8. It should return 9.
When i add another month in line 2, it turns the month to October. When i retrieve the month, it gives back value 9. Actually October
is the 10th month. Why is that?

Arhaan
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaDoc -- java.util.Date -- explains it... basically, the first month is month zero.

Henry


Arhaan Singhania wrote:Hi,
I took this example from Chapter 6 K&B. Here is the code



When i call it, it shows the date is 09 September but when i try to retrieve the month in line one, it returns 8. It should return 9.
When i add another month in line 2, it turns the month to October. When i retrieve the month, it gives back value 9. Actually October
is the 10th month. Why is that?

Arhaan
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the documentation for Calendar. Under the MONTH constant:
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.

Confusing? Yes .
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Henry/Mathew,

That was very helpful. I should first check the documentation, my fault .

Mathews, it was confusing definitely, just wondering, why would they keep it to zero

Thanks,
Arhaan
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arhaan Singhania wrote:Mathews, it was confusing definitely, just wondering, why would they keep it to zero

I can't be sure, but it's probably something to do with the zero-indexing of arrays/lists. It just happens to be so confusing because it goes against standard practice with dates, which everyone is familiar with.

One way to cope is to always use Calendar.JANUARY as an offset. E.g. if you want to set the month to what you think of as the 9th month, you could use calendar.set(Calendar.MONTH, 8 + Calendar.JANUARY), and it would work regardless of how it had been implemented. When you come back to the same code a year later, you won't have to remember why it's off by one.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mathew,

Thanks a ton. It will help me remember to take MONTH as an offset. really helpful. Thanks a ton.

Arhaan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic