Convert java.util.Date to java.time.LocalDate

前端 未结 13 2585
温柔的废话
温柔的废话 2020-11-22 08:45

What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate?

Date input = new Date();
Loca         


        
13条回答
  •  爱一瞬间的悲伤
    2020-11-22 09:44

    If you're using Java 8, @JodaStephen's answer is obviously the best. However, if you're working with the JSR-310 backport, you unfortunately have to do something like this:

    Date input = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(input);
    LocalDate date = LocalDate.of(cal.get(Calendar.YEAR),
            cal.get(Calendar.MONTH) + 1,
            cal.get(Calendar.DAY_OF_MONTH));
    

提交回复
热议问题