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

女生的网名这么多〃 提交于 2019-12-01 06:06:36

问题


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?

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/26255636/why-does-java-util-date-represent-year-as-year-1900

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!