In this article, we will see how to convert String to LocalDateTime in Java.
LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default.
Java String to LocalDateTime
To convert String to LocalDateTime in Java, LocalDateTime provides static method parse()
which takes String as input and returns LocalDateTime object.
1 2 3 |
public static LocalDateTime parse(CharSequence text) |
parse()
method accepts a date time String in ISO-8601 by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.time.LocalDateTime; public class StringToLocalDateTime { public static void main(String[] args) { // ISO-8601 formatted String String dateStr = "2022-02-16T10:22:15"; // Convert String to LocalDateTime using Parse() method LocalDateTime localDateTime = LocalDateTime.parse(dateStr); // Print LocalDateTime object System.out.println("LocalDateTime obj: "+localDateTime); } } |
Output:
In case, if you provide String in any other format, it will throw DateTimeParseException
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.time.LocalDateTime; public class StringToLocalDateTime { public static void main(String[] args) { // ISO-8601 formatted String String dateStr = "2022-02-16 10:22:15"; // Convert String to LocalDateTime using Parse() method LocalDateTime localDateTime = LocalDateTime.parse(dateStr); // Print LocalDateTime object System.out.println("LocalDateTime obj: "+localDateTime); } } |
Output:
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.LocalDateTime.parse(LocalDateTime.java:492)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:477)
at org.arpit.java2blog.StringToLocalDateTime.main(StringToLocalDateTime.java:13)
Convert String to LocalDateTime with custom format
To convert String to LocalDateTime with custom format, we can use DateTimeFormatter
.
- Create DateTimeFormatter instance with specified format
- Pass date formatted String and DateTimeFormatter instance to LocalDateTime’s
parse()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class StringToLocalDateTime { public static void main(String[] args) { // Custom formatted String String dateStr = "2022-02-16 10:22"; // Create DateTimeFormatter instance with specified format DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); // Convert String to LocalDateTime using Parse() method LocalDateTime localDateTime = LocalDateTime.parse(dateStr,dateTimeFormatter); // Print LocalDateTime object System.out.println("LocalDateTime obj: "+localDateTime); } } |
Output:
Further reading:
That’s all about how to convert String to LocalDateTime in Java.