Join 3,374 readers in helping fund MetaFilter (Hide)


When I say year 2000, you say 3900, wait, what?
December 2, 2011 2:34 PM   Subscribe

Recently doing more and more work in java and am wondering why was Java's getYear function was deprecated in favor of the seemingly more cumbersome and error prone "Calendar.get(Calendar.Year) - 1900" ?

It seems to run counter to the modern paradigm that methods should be named after what the do, and seems like it would generate a lot of wasted redundant code since every instance where someone wants the year from their date object, they really do want the year, not the year + 1900 that they have to manually subtract the extra 1900 from.

So, was there a compelling reason between JDK 1.0 and 1.1 that makes sense out of this change?

Bonus question, was there any pushback from the java community at the time over the change?
posted by nomisxid to Computers & Internet (7 answers total)
 
I very strongly recommend you use the joda library instead of Date or Calendar, which are horrible.
posted by Zarkonnen at 2:52 PM on December 2, 2011 [2 favorites]


Look at the API docs, thats not at all how you should be calling it. More like:
Calendar toDay = Calendar.getInstance();
int year = toDay.get(Calendar.YEAR);
And that returns the proper year.
posted by H. Roark at 2:53 PM on December 2, 2011


You can find the Joda package that Zarkonnen suggests here.
posted by asphericalcow at 2:58 PM on December 2, 2011 [1 favorite]


Interesting, it's changed multiple times looks like.
posted by nomisxid at 3:03 PM on December 2, 2011


Actually, the next version of Java, scheduled for the end of 2012, is going to introduce yet another date handling implementation because, frankly, Java's date handling still completely sucks.

So that's why the original is deprecated in lieu of the somewhat-better Calendar implementation.
posted by mikeh at 4:06 PM on December 2, 2011


And H. Roark is right, but you probably knew that and just left out the .getInstance(), since .get() isn't static and probably wouldn't work that way, anyway.
posted by mikeh at 4:33 PM on December 2, 2011


seems like it would generate a lot of wasted redundant code since every instance where someone wants the year from their date object, they really do want the year, not the year + 1900 that they have to manually subtract the extra 1900 from.
This is backwards. Date.getYear() would not return the year; it would return the year minus 1900 (the reason for this is because it's essentially how the C language behaved, and C pervaded into many, many other things).

So when they say "replaced by Calendar.get(Calendar.YEAR) - 1900", they don't mean Calendar.get(Calendar.YEAR) returns the year plus 1900; they mean it returns the year, and if you want to emulate Date.getYear(), you would therefore have to subtract 1900 from it.
So, was there a compelling reason between JDK 1.0 and 1.1 that makes sense out of this change?
The reason given in the documentation for the Date class: "Unfortunately, the API for these functions was not amenable to internationalization."

For example, the newer methodology more easily support locales that do not use the Gregorian calendar.
posted by Flunkie at 8:45 PM on December 2, 2011


« Older MeFites, what is your favorite...   |  Can I/how do I fall back in lu... Newer »
This thread is closed to new comments.