Java 8 – How to parse date with LocalDateTime

Here are a few Java 8 examples to parse date with LocalDateTime.

First, find the DateTimeFormatter pattern that matches the date format, for example:


  String str = "2020-01-30 12:30:41";
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

second, parse it with LocalDateTime.parse


  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  String str = "2020-01-30 12:30:41";
  LocalDateTime localDateTime = LocalDateTime.parse(str, dtf);

1. 2020-01-30 12:30:41

JavaDateTimeExample1.java

package com.mkyong.time;

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

public class JavaDateTimeExample1 {

    public static void main(String[] args) {

        String str = "2020-01-30 12:30:41";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // String -> LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.parse(str, dtf);

        // LocalDateTime -> String
        String result = localDateTime.format(dtf);

        System.out.println(result);

    }
}

Output


2020-01-30 12:30:41

2. 31-Aug-2020

2.1 By default, DateTimeFormatter will use the default JVM locale to parse the date. In this example, the -Aug- is an English word, if the JVM default locale is US, everything is fine.

JavaDateTimeExample2.java

package com.mkyong.time;

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

public class JavaDateTimeExample2 {

    public static void main(String[] args) {

        // default jvm locale
        System.out.println(Locale.getDefault()); //en_US

        String str = "31-Aug-2020";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-yyyy");

        // String -> LocalDateTime
        LocalDateTime localDateTime = LocalDate.parse(str, dtf).atStartOfDay();

        // LocalDateTime -> String
        String result = localDateTime.format(dtf);

        System.out.println(result);

    }
}

Output


en_US
31-Aug-2020

2.2 Now the DateTimeParseException will be thrown if we change the JVM default locale to CHINA.


  Locale.setDefault(Locale.CHINA);
  String str = "31-Aug-2020";
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-yyyy");
  LocalDateTime localDateTime = LocalDate.parse(str, dtf).atStartOfDay();

Output


Exception in thread "main" java.time.format.DateTimeParseException: Text '31-Aug-2020' could not be parsed at index 3
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
	at java.base/java.time.LocalDate.parse(LocalDate.java:428)

To solve it, defined the correct locale in DateTimeFormatter.

JavaDateTimeExample3.java

package com.mkyong.time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class JavaDateTimeExample3 {

    public static void main(String[] args) {

        Locale.setDefault(Locale.CHINA);

        String str = "31-Aug-2020";

        // don't care about the JVM default locale, we use Locale.US to parse date
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US);

        // String -> LocalDateTime
        LocalDateTime localDateTime = LocalDate.parse(str, dtf).atStartOfDay();

        // LocalDateTime -> String
        String result = localDateTime.format(dtf);

        System.out.println(result);

    }
}

Output


31-Aug-2020

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments