【date】java Date getYear()獲取年份問題
阿新 • • 發佈:2019-02-18
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。