2

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;
}
amal
  • 3,470
  • 10
  • 29
  • 43
  • A `Date` doesn't have a format. – Sotirios Delimanolis Jun 04 '15 at 18:08
  • @SotiriosDelimanolis I didn't get you. Can you explain little more please. – amal Jun 04 '15 at 18:12
  • 1
    You say _return Date type object with the format MM-dd-yyyy_. A `Date` object is just a timestamp. It doesn't have a format. If you want a format, use a `SimpleDateFormat` and `format` the `Date`. – Sotirios Delimanolis Jun 04 '15 at 18:12
  • @SotiriosDelimanolis If you check the getStartDate() method below, you will see i have LocalDate object. But method is returning Date type object. So what i want to do is, convert localDate object to util Date object. – amal Jun 04 '15 at 18:15

2 Answers2

4

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

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;
    }
meteor
  • 2,518
  • 4
  • 38
  • 52
  • Thank you for response. But it didnt work. I printed localDate and it was: 1907-01-01. Then I printed "date" and it was like this: Tue Jan 01 00:00:00 EST 1907 – amal Jun 04 '15 at 18:29
  • Reason for downvote? Date is an object as mentioned by @SotiriosDelimanolis response in comment above! If you need to display it as formatted string with MM-dd-yyy format then at the place where you make a call to getStartDate() method use something like `SimpleDateFormat wanted = new SimpleDateFormat("MM-dd-yyyy"); wanted.format(date.toString());` – meteor Jun 04 '15 at 18:43