In this article, we will learn how to convert String to LocalDateTime in Java 1.8 version using different date formats
Q) What is the need of converting String to LocalDateTime ?
- Generally, whenever we receive any data from web application then it is passed in the form of String
- But for further processing we need to convert String to LocalDateTime
- This article explains about String to LocalDateTime conversion in different formats
Read also,
- String to Date conversion in different formats
- Date to String conversion is also required for reverse process
- For LocalDateTime to String conversion, read Java 8 – How to convert LocalDateTime to String ?
1. String to LocalDateTime conversion:
We will use below formats to convert String to LocalDateTime,
- yyyy-MM-ddTHH:mm:ss.nnn (default)
- dd-MM-yyyy hh:mm:ss.nnn a
- dd.MM.yyyy HH:mm:ss
- dd/MMM/yyyy hh:mm:ss a
- E, MMM dd yyyy HH:mm
- MMM dd, yyyy hh:mm a
- MM/dd, yyyy hh:mm a
Note: for creating above formats we need DateTimeFormatter
2. Convert String to LocalDateTime examples:
2.1 Convert String in default (yyyy-MM-ddTHH:mm:ss.nnn) format to LocalDateTime :
ConvertStringToLocalDateTime1.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
public class ConvertStringToLocalDateTime1 {
public static void main(String[] args) {
// 1. string
String strDateTime = "2017-05-29T19:07:34.190912345";
// 2. parse date/time in String to default LocalDateTime format
LocalDateTime localDate = LocalDateTime.parse(strDateTime);
// 3. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in default (yyyy-MM-ddTHH:mm:ss.nnn) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
2017-05-29T19:07:34.190912345
String in default (yyyy-MM-ddTHH:mm:ss.nnn) format to LocalDateTime :-
2017-05-29T19:07:34.190912345
2.2 Convert String in (dd-MM-yyyy hh:mm:ss.nnn a) format to LocalDateTime :
ConvertStringToLocalDateTime2.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateTime2 {
public static void main(String[] args) {
// 1. string
String strDateTime = "29-05-2017 05:07:34.190912345 pm";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd-MM-yyyy hh:mm:ss.nnn a");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in (dd-MM-yyyy hh:mm:ss.nnn a) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
29-05-2017 05:07:34.190912345 pm
String in (dd-MM-yyyy hh:mm:ss.nnn a) format to LocalDateTime :-
2017-05-29T17:07:34.190912345
2.3 Convert String in (dd.MM.yyyy HH:mm:ss) format to LocalDateTime :
ConvertStringToLocalDateTime3.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateTime3 {
public static void main(String[] args) {
// 1. string
String strDateTime = "29.05.2017 10:07:34";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd.MM.yyyy HH:mm:ss");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in (dd.MM.yyyy HH:mm:ss) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
29.05.2017 10:07:34
String in (dd.MM.yyyy HH:mm:ss) format to LocalDateTime :-
2017-05-29T10:07:34
2.4 Convert String in (dd/MMM/yyyy hh:mm:ss a) format to LocalDateTime :
ConvertStringToLocalDateTime4.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateTime4 {
public static void main(String[] args) {
// 1. string
String strDateTime = "29/May/2017 10:07:34 pm";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd/MMM/yyyy hh:mm:ss a");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in (dd/MMM/yyyy hh:mm:ss a) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
29/May/2017 10:07:34 pm
String in (dd/MMM/yyyy hh:mm:ss a) format to LocalDateTime :-
2017-05-29T22:07:34
2.5 Convert String in (E, MMM dd yyyy HH:mm) format to LocalDateTime :
ConvertStringToLocalDateTime5.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateTime5 {
public static void main(String[] args) {
// 1. string
String strDateTime = "Tue, Aug 09 2022 11:17";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("E, MMM dd yyyy HH:mm");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in (E, MMM dd yyyy HH:mm) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
Tue, Aug 09 2022 11:17
String in (E, MMM dd yyyy HH:mm) format to LocalDateTime :-
2022-08-09T11:17
2.6 Convert String in (MMM dd, yyyy hh:mm a) format to LocalDateTime :
ConvertStringToLocalDateTime6.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateTime6 {
public static void main(String[] args) {
// 1. string
String strDateTime = "Aug 09, 2022 11:17 pm";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("MMM dd, yyyy hh:mm a");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in (MMM dd, yyyy hh:mm a) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
Aug 09, 2022 11:17 pm
String in (MMM dd, yyyy hh:mm a) format to LocalDateTime :-
2022-08-09T23:17
2.7 Convert String in (MM/dd, yyyy hh:mm a) format to LocalDateTime :
ConvertStringToLocalDateTime7.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateTime7 {
public static void main(String[] args) {
// 1. string
String strDateTime = "08/09, 2022 11:17 pm";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("MM/dd, yyyy hh:mm a");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.print("\nString in (MM/dd, yyyy hh:mm a) format "
+ "to LocalDateTime :- \n"
+ localDate);
}
}
Output:
Original String :-
08/09, 2022 11:17 pm
String in (MM/dd, yyyy hh:mm a) format to LocalDateTime :-
2022-08-09T23:17
3. DateTimeParseException:
- If the specified String doesn’t match with the specified pattern/format then DateTimeParseException is thrown
- Use below date/time notation,
- a – for am/pm marker
- z – regional time-zone like IST or PST or CET
- XXX – for offset from GMT/UTC
- ss – seconds
- mm – minutes
- hh – 12-hour format
- HH – 24 hour format
- y/yyyy– year with all 4 digits
- yy – year with last 2 digits
- MM – month in numbers
- MMM – month in words in short form
- MMMM – month in words in full form
- dd – date
- E/EEE – day of week in short form
- EEEE – day of week in full form
StringToLocalDateTimeConversion.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class StringToLocalDateTimeConversion {
public static void main(String[] args) {
// 1. string
String strDateTime = "16-02-1987 10:07:34";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd/MM/yyyy HH:mm:ss");
// 3. parse date in String format using above dateTimeFormatter
LocalDateTime localDate = LocalDateTime
.parse(strDateTime, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDateTime);
System.out.println("\nString in (dd/MM/yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '16-02-1987 10:07:34' could not be parsed at index 2
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
at in.bench.resources.java8.localdatetime.examples.StringToLocalDateTimeConversion
.main(StringToLocalDateTimeConversion.java:21)
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!