1

In java if I have a String that looks like this: "Nov 30, 2016" (this is FormatStyle.MEDIUM), how do I convert it into a LocalDateTime data type?

ronjdv
  • 21
  • 1
  • 4

1 Answers1

0

I ran this on JDK 8:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class DateFormatDemo {
    public static void main(String[] args) {
        String dateStr = "Nov 30, 2016";
        LocalDateTime ldt = LocalDateTime.parse(dateStr, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
        System.out.println("date string: " + dateStr);
        System.out.println("local date : " + ldt);
    }
}

Here's my output:

date string: Nov 30, 2016
local date : 2016-11-30

Process finished with exit code 0

I got an exception with the original code using LocalDateTime, because the input String didn't include a time.

If you'd like LocalDateTime, try adding hh:mm:ss to the input.

duffymo
  • 305,152
  • 44
  • 369
  • 561