aspose file tools*
The moose likes Beginning Java and the fly likes Year in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Year in Java" Watch "Year in Java" New topic
Author

Year in Java

Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
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?


The greater danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Dec 29, 2008
Posts: 162
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
thanks Rob for your reply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Year in Java