Date that accept or r...">

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, a method getYear()

etc. were deprecated in favor java.util.Calendar

, which still have this weird note of 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 a month 0

, but we all know that (although you think they removed this issue 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 have checked the following questions:

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

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

My question is:

  • Perhaps the original creators java.util.Date

    could get the "year" from the data storage, subtracting 1900 from it? Especially if it is mostly stored as long.

Thus:

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 ?
+3


source to share


2 answers


Mostly the original designers of java.util.Date copied a lot from C. What you see is the result of this - see tm

struct
. So you should probably ask why this was intended to be used in 1900. I suspect the fundamental answer is "because we weren't very good at API design when it was developed tm

." My point is that we are 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 an API, not a storage format internally java.util.Date

. No less annoying, mind you.

+10


source


java.util.Date has no date at all. This (quoting http://docs.oracle.com/javase/6/docs/api/java/util/Date.html ) a specific point in time with millisecond precision.

It has no relationship with any particular date, hour, etc. You can extract the day, year, etc. from it. - using the given calendar and time zone. Differential calendars, time zones will give different dates.

If you are ever interested in keeping the date (day, month, year) then don't use java.util.Date



Instead

+2


source







All Articles