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?
Asked
Active
Viewed 980 times
1 Answers
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
-
I copy pasted your code and this is not working: 'Exception in thread "main" java.time.format.DateTimeParseException: Text 'Nov 30, 2016' could not be parsed at index 0' – Georgios Aug 09 '19 at 14:04
-
You get that error if you try to parse a date without time as LocalDateTime. – duffymo Aug 09 '19 at 15:06
-
How would then your code look like without errors? – Georgios Aug 09 '19 at 15:09
-
Just as I modified above. That works for me. If you want LocalDateTime, please do some work for yourself. – duffymo Aug 09 '19 at 15:10
-
Nope, just LocalDate. The edit is still not working. I run it with **jdk-11.0.1.** – Georgios Aug 09 '19 at 15:27