Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java.util.Date represent Year as "year-1900"?

In java.util.Date:

 * In all methods of class <code>Date</code> that accept or return
 * year, month, date, hours, minutes, and seconds values, the
 * following representations are used:
 * <ul>
 * <li>A year <i>y</i> is represented by the integer
 *     <i>y</i><code>-1900</code>.

Of course, in Java 1.1, the getYear() method and the like were deprecated in favor of java.util.Calendar, which still has this weird deprecation note:

 int    getYear() 
    Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

 setYear(int year) 
      Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.YEAR, year + 1900).

And of course, Month is 0-based but we all know that (although you'd think they had removed that problem from Calendar - they didn't):

 * <li>A month is represented by an integer from 0 to 11; 0 is January,
 *     1 is February, and so forth; thus 11 is December.

I did check the following questions:

Why does Java's Date.getYear() return 111 instead of 2011?

Why is the Java date API (java.util.Date, .Calendar) such a mess?

My question is:

  • What possibly could have the original creators of java.util.Date hoped to gain from storing the data of "year" by subtracting 1900 from it? Especially if it's basically stored as a long.

As such:

private transient long fastTime;

@Deprecated
public int getYear() {
    return normalize().getYear() - 1900;
} 

@Deprecated
public void setYear(int year) {
    getCalendarDate().setNormalizedYear(year + 1900);
}

private final BaseCalendar.Date getCalendarDate() {
    if (cdate == null) {
        BaseCalendar cal = getCalendarSystem(fastTime);
    ....
  • Why 1900?
like image 606
EpicPandaForce Avatar asked Oct 08 '14 11:10

EpicPandaForce


2 Answers

Basically the original java.util.Date designers copied a lot from C. What you're seeing is the result of that - see the tm struct. So you should probably ask why that was designed to use the year 1900. I suspect the fundamental answer is "because we weren't very good at API design back when tm was designed." I'd contend that we're still not very good at API design when it comes to dates and times, because there are so many different use cases.

This is just the API though, not the storage format inside java.util.Date. No less annoying, mind you.

like image 142
Jon Skeet Avatar answered Oct 08 '22 19:10

Jon Skeet


java.util.Date is no date at all. It is (quoting http://docs.oracle.com/javase/6/docs/api/java/util/Date.html) specific instant in time, with millisecond precision.

It has no relationship with any particular date, hour, etc. You may extract day, year, etc from it- using given calendar and timezone. Diffrent calendars, timezones will give diffrent dates.

If you are ever interested in storing date (day, month, year) do not use java.util.Date

Instead

  • org.joda.time.DateTime from http://www.joda.org/joda-time/
  • if you are on java8, use java.time package http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
like image 40
Bartosz Bilicki Avatar answered Oct 08 '22 18:10

Bartosz Bilicki