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

10,118

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.

Share:
10,118
amal
Author by

amal

Updated on June 04, 2022

Comments

  • amal
    amal almost 2 years

    I have java.time.LocalDate Object in yyyy-MM-dd format. I would like to know how to convert this to java.util.Date with MM-dd-yyyy format. getStartDate() method should be able to return Date type object with the format MM-dd-yyyy.

    DateParser class

    package com.accenture.javadojo.orgchart;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Locale;
    
    
        public class DateParser {
    
            public static LocalDate parseDate(String strDate){
    
                try{
                    if((strDate != null) && !("").equals(strDate)){
    
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/yyyy").withLocale(Locale.US);
                        LocalDate date = LocalDate.parse(strDate, formatter);
                        return date;
                    }
                } catch (DateTimeParseException e) {
    
                    e.printStackTrace();
                }
    
                return null;
            }
    
        }
    

    public Date getStartDate() {
    
        String fmd = format.format(startDate); 
    
        LocalDate localDate = DateParser.parseDate(fmd);
    
        return startDate;
    }