今天在写程序时,用到了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 <code>Date</code> 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 <code>Calendar.get(Calendar.YEAR) - 1900</code>.
*/
@Deprecated
public int getYear() {
return normalize().getYear() - 1900;
}
用这个方法获取年份时是从1900年开始计算的,因此当年份为2009时,得到的结果为109,所以如果要得到最终的年份,要再加上1900。