java.util.Date and getYear()

前端 未结 12 1121
生来不讨喜
生来不讨喜 2020-12-08 13:01

I am having the following problem in Java (I see some people are having a similar problem in JavaScript but I\'m using Java)

System.out.println(new Date().ge         


        
12条回答
  •  执笔经年
    2020-12-08 13:23

    According to javadocs:

    @Deprecated
    public int getYear()
    

    Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 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.

    Returns: the year represented by this date, minus 1900.

    See Also: Calendar

    So 112 is the correct output. I would follow the advice in the Javadoc or use JodaTime instead.

提交回复
热议问题