• 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

Year in Java

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
Why the year in Java is 1900 less than the actual one by default? what is the specific reaosn for that?
And also why is the month starts from 0-11 instead of 1-12?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sujai Kaarthik wrote:Hi guys,
Why the year in Java is 1900 less than the actual one by default? what is the specific reaosn for that?
And also why is the month starts from 0-11 instead of 1-12?



When using the Calendar class, use the constants instead of magic numbers. For instance, use Calendar.JANUARY instead of 0, and you won't have to worry about what numbers are being used to represent these months. Not sure what you mean by your first question. Can you post compilable/runnable code to demonstrate it?
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When using the Calendar class, use the constants instead of magic numbers. For instance, use Calendar.JANUARY instead of 0, and you won't have to worry about what numbers are being used to represent these months. Not sure what you mean by your first question. Can you post compilable/runnable code to demonstrate it?



I have no code to compile/run
I know to use those constants, but I was thinking why the default year is less than 1900 from the current year and why the month starts from 0-11 and why not 1-12.. is there any specific reasons for that?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calendar does not have that "based on 1900" thingy - when you retrieve its year you'll get 2009 instead of 109. Basing java.util.Date's year on 1900 was a design decision, probably inspired by the thought that people would never need dates before 1900. That's why retrieving years etc is deprecated in Date - you should use Calendar for that:


As for why the months are 0 based, that probably has something to do with arrays. Arrays in Java are all 0 based, so Calendar.JANUARY would be the index of the first element of the array.
Now of course Sun screwed up that line of thought by having the weekdays start at Calendar.SUNDAY - which is 1, not 0. DateFormatSymbols just adds an empty element in the weekday arrays. Go figure...
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob for your reply
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic