• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Year in Java

 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks Rob for your reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic