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

我是研究僧i 提交于 2019-12-06 12:52:01

If you have a LocalDate which you want to convert to a Date, use

LocalDate localDate = ...;
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

source: http://blog.progs.be/542/date-to-java-time

You can then use a SimpleDateFormat to format the Date to whatever format you like.

You can use the SimpleDateFormat to switch between LocalDate and Date objects.

import java.text.SimpleDateFormat;

public Date getStartDate() {

        String fmd = format.format(startDate); 

        LocalDate localDate = DateParser.parseDate(fmd);

        SimpleDateFormat actual = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat wanted = new SimpleDateFormat("MM-dd-yyyy");

        String reformatted = wanted.format(actual.parse(localDate.toString()));
        Date date = wanted.parse(reformatted);
        return date;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!