java.util.Date and getYear()

前端 未结 12 1111
生来不讨喜
生来不讨喜 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:31

    Yup, this is in fact what's happening. See also the Javadoc:

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

    The getYear method is deprecated for this reason. So, don't use it.

    Note also that getMonth returns a number between 0 and 11. Therefore, this.sale.getSaleDate().getMonth() returns 1 for February, instead of 2. While java.util.Calendar doesn't add 1900 to all years, it does suffer from the off-by-one-month problem.

    You're much better off using JodaTime.

提交回复
热议问题