【date】java Date getYear()获取年份问题

java.util.Date类中的getYear()方法,发现取出来的年份是109,与真实年份差1900。查看了相关文档知道了原因。

/**
     * Returns a value that is the result of subtracting 1900 from the 
     * year that contains or begins with the instant in time represented 
     * by this Date object, as interpreted in the local 
     * time zone.
     *
     * @return  the year represented by this date, minus 1900.
     * @see     java.util.Calendar
     * @deprecated As of JDK version 1.1,
     * replaced by Calendar.get(Calendar.YEAR) - 1900.
     */
    @Deprecated
    public int getYear() {
        return normalize().getYear() - 1900;
    }

    用这个方法获取年份时是从1900年开始计算的,因此当年份为2009时,得到的结果为109,所以如果要得到最终的年份,要再加上1900。

你可能感兴趣的:(Java基础Base,Java)