*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calendar And Date class Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calendar And Date class Doubt" Watch "Calendar And Date class Doubt" New topic
Author

Calendar And Date class Doubt

Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Mar 28, 2011
Posts: 89
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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Mar 28, 2011
Posts: 89
Dear Mathew,

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

Arhaan
 
 
subject: Calendar And Date class Doubt
 
Similar Threads
Unable to run this code in EditPlus
Number of weeks between two dates
One More to the K&B Errata log...Ch 6 Dates related
Holiday in date
please help me ...