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.favtuts.time;

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

public class ParseStringLocalDateTime {

    public static void main(String[] args) {
        parseStringyyyyMMddHHmmss();
    }

    static void parseStringyyyyMMddHHmmss() {
        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.favtuts.time;

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

public class ParseStringLocalDateTime {

    public static void main(String[] args) {
        parseDateDefaultLocaleUS();
    }

    static void parseDateDefaultLocaleUS() {
        // 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)
        at com.favtuts.time.ParseStringLocalDateTime.parseDateFailedWithLocaleChina(ParseStringLocalDateTime.java:20)
        at com.favtuts.time.ParseStringLocalDateTime.main(ParseStringLocalDateTime.java:13)

To solve it, defined the correct locale in DateTimeFormatter.

JavaDateTimeExample3.java

package com.favtuts.time;

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

public class ParseStringLocalDateTime {

    public static void main(String[] args) {
        parseDateSuccessWithLocaleChina();
    }

    static void parseDateSuccessWithLocaleChina() {
        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

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-basic/time

References

Leave a Reply

Your email address will not be published. Required fields are marked *