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

前端 未结 13 2438
温柔的废话
温柔的废话 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:46

    Date input = new Date();
    LocalDateTime  conv=LocalDateTime.ofInstant(input.toInstant(), ZoneId.systemDefault());
    LocalDate convDate=conv.toLocalDate();
    

    The Date instance does contain time too along with the date while LocalDate doesn't. So you can firstly convert it into LocalDateTime using its method ofInstant() then if you want it without time then convert the instance into LocalDate.

提交回复
热议问题