java.util.Date and getYear()

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

    Java 8 LocalDate class is another option to get the year from a java.util.Date,

    int year = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date)).getYear();
    

    Another option is,

    int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
    

提交回复
热议问题