• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I can't seem to get gredorian calender working right

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi the code below will result in this being printed to screen:

Today Year: 2005
Today Month: 8
Today Day: 13
Today Hour: 8
Today Minute: 59
Today date: 2005-8-13

Yet the actual date is 2005-9-13. Can anyone tell me why the month keeps comeing out wrong?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the month-field of Calendar is zero-based, Calendar.JANUARY == 0. Adding 1 should do the trick.

Why, I'm not sure of.
 
Chaz Anderson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is only the month 0 based?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Date and Calendar classes the month is defined to be in the interval [0..11], not [1..12] as it in the real world. So month 8 is September, not August.

why is only the month 0 based?



Because it would be very confusing to redefine days or years (which are numbers) to use different ones. Months at least have a non-numerical designation (i.e., a name) which is unique. Of course, it is still very confusing.
Another explanation would be that Java array indices start at 0, and the names of months are stored in an array internally somewhere, so a Sun developer who wrote these classes got a bit lazy, and/or thought that it makes sense this way.
[ September 13, 2005: Message edited by: Ulf Dittmer ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic