How to Convert String to LocalDateTime in Java 8 - Example Tutorial

Suppose you have a date time String "2016-03-04: 11:01:20" and you want to convert this into a LocalDateTime object of Java 8 new date and time API, how do you do that? Well, if you have worked previously with String and Date then you know that you can parse String to Date in Java. Prior to Java 8, we used to use SimpleDateFormat, which was mutable and not thread-safe, and advised not to be shared between threads. In Java 8, you can use the DateTimeFormatter class to convert String to LocalDate, LocalTime or LocalDateTime. Unlike SimpleDateFormat, the DateTimeFormatter is immutable and thread-safe and you can use it as a static variable for sharing and reuse. It also comes with pre-defined date time format e.g. ISO-8601 format. The ISO_LOCAL_DATE_TIME formats or parses a date-time without an offset, such as '2016-03-04T10:15:30'.

In this article, I'll show you how to convert a String to LocalDateTime in Java 8, and then back to a formatted String, but you can also check out Java SE 8 for Really Impatient to learn more about new Date and Time API, which is quite vast.

Java 8 String to Date conversion example

Converting String to LocalDateTime

To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a String and a DateTimeFormatter as a parameter. The DateTimeFormatter is used to specify the date/time pattern. You can use predefined format e.g. ISO_LOCAL_DATE_TIME or can specify by yourself as String into the second parameter. If you are new to formatting instructions, see here.

String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

If your String contains seconds as well e.g. "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:

String str = "2016-03-04 11:30: 40";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Here is is another example of formatting dates in Java 8:

DateTimeFormatter Example in Java 8

That's all about how to convert String to LocalDateTime in Java 8. As I said, you can use the java.time.DateTimeFormatter class for parsing and formatting String in Java 8. Unlike DateFormat and SimpleDateFormat of old date and time API, this class is both immutable and thread-safe, and you can also store it into a static variable.

In the next part of this tutorial, I'll show you how to convert LocalDateTime to String in Java by formatting date into different patterns.

Further Learning
The Ultimate Java 8 Tutorial
Java SE 8 New Features
From Collections to Streams in Java 8 Using Lambda Expressions

Other Date and Time Tutorials for curious developers:
  • How to convert util Date to LocalDateTime in Java 8? (solution)
  • How to convert java.util.Date to java.sql.Date in JDBC? (solution)
  • How to display dates in different timezones in Java? (answer)
  • How to parse String to Date in multithreading using Joda Time? (example)
  • How to get the current date and time in Java? (example)

No comments:

Post a Comment