Why Java util. Date represents year as “year-1900”?

In Java util. In 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, it is not recommended to use getyear () method to support Java util. Calendar, it still has this strange 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).

Of course, month is based on 0, but we all know (although you think they have deleted the problem from the calendar - they don'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:

> java. util. Might the original creator of date want to store data for the "year" by subtracting 1900 from it? Especially if it is basically stored as long

Therefore:

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?

Solution

Basically the original Java util. Date designer copied a lot from C What you see is the result – see TM struct So you should ask why that design was used in 1900 I suspect the fundamental answer is "because we are not very good at API design when designing TM." I think we are still not good at API design in terms of date and time because there are too many different use cases

This is just an API, not Java util. Storage format in date Equally annoying, please note

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>