为什么java.util.Date将Year表示为“year-1900”?
发布时间:2020-12-15 05:12:11 所属栏目:Java 来源:网络整理
导读:在 java.util.Date中: * In all methods of class codeDate/code that accept or return * year,month,date,hours,minutes,and seconds values,the * following representations are used: * ul * liA year iy/i is represented by the integer * iy/icode-
在
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>. 当然,在Java 1.1中,不推荐使用getYear()方法等来支持java.util.Calendar,它仍然有这个奇怪的弃用说明: 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). 当然,Month是基于0但我们都知道(尽管你认为他们已经从日历中删除了这个问题 – 他们没有): * <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. 我确实检查了以下问题: Why does Java’s Date.getYear() return 111 instead of 2011? Why is the Java date API (java.util.Date,.Calendar) such a mess? 我的问题是: > java.util.Date的原始创建者可能希望通过从中减去1900来存储“年份”的数据?特别是如果它基本上存储为长. 因此: 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); .... >为什么选择1900? 解决方法
基本上原始的java.util.Date设计师从C中复制了很多.你看到的是结果 – 见
tm struct.所以你应该问为什么那个设计用于1900年.我怀疑根本答案是“因为我们在设计tm时不太擅长API设计.”我认为,就日期和时间而言,我们仍然不擅长API设计,因为有太多不同的用例.
这只是API,而不是java.util.Date中的存储格式.同样烦人,请注意. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |