In this article, we will learn how to convert LocalDateTime in different formats using Java 1.8 version or in short how to convert LocalDateTime into String-form
For String to LocalDateTime conversion, read Java 8 – How to convert String to LocalDateTime ?
1. Convert LocalDateTime in different formats :
- We can convert default ISO_LOCAL_DATE_TIME format (yyyy-MM-ddTHH:mm:ss.nnn) to any other formats using LocalDateTime.format() method by passing DateTimeFormatter as argument with required pattern in String-form
- In below illustration, we are using 7 different custom formats as mentioned below,
- DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss.nnn“)
- DateTimeFormatter.ofPattern(“dd-MM-yyyy hh:mm:ss.nnn a“)
- DateTimeFormatter.ofPattern(“dd.MM.yyyy HH:mm:ss“)
- DateTimeFormatter.ofPattern(“dd/MMM/yyyy hh:mm:ss a“)
- DateTimeFormatter.ofPattern(“E, MMM dd yyyy HH:mm“)
- DateTimeFormatter.ofPattern(“MMM dd, yyyy hh:mm a“)
- DateTimeFormatter.ofPattern(“MM/dd, yyyy hh:mm a“)
- Note: If the specified custom format/pattern isn’t in correct form then DateTimeParseException is thrown
2. LocalDateTime examples in different formats :
2.1 LocalDateTime to (yyyy-MM-dd HH:mm:ss.nnn) format :
- This example converts LocalDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn) format to (yyyy-MM-dd HH:mm:ss.nnn) format
FormatLocalDateTimeExample1.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample1 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.nnn");
// 3. LocalDateTime to (yyyy-MM-dd HH:mm:ss.nnn) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (yyyy-MM-dd HH:mm:ss.nnn) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:36:17.525706
LocalDateTime to (yyyy-MM-dd HH:mm:ss.nnn) format :-
2022-08-09 06:36:17.525706000
2.2 LocalDateTime to (dd-MM-yyyy hh:mm:ss.nnn a) format :
- This example converts LocalDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn) format to (dd-MM-yyyy hh:mm:ss.nnn a) format
FormatLocalDateTimeExample2.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample2 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd-MM-yyyy hh:mm:ss.nnn a");
// 3. LocalDateTime to (dd-MM-yyyy hh:mm:ss.nnn a) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (dd-MM-yyyy hh:mm:ss.nnn a) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:36:33.806022800
LocalDateTime to (dd-MM-yyyy hh:mm:ss.nnn a) format :-
09-08-2022 06:36:33.806022800 am
2.3 LocalDateTime to (dd.MM.yyyy HH:mm:ss) format :
- This example converts LocalDateTime in default (dd.MM.yyyy HH:mm:ss) format to (dd-MM-yyyy hh:mm:ss.nnn a) format
FormatLocalDateTimeExample3.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample3 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd.MM.yyyy HH:mm:ss");
// 3. LocalDateTime to (dd.MM.yyyy HH:mm:ss) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (dd.MM.yyyy HH:mm:ss) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:36:48.668339400
LocalDateTime to (dd.MM.yyyy HH:mm:ss) format :-
09.08.2022 06:36:48
2.4 LocalDateTime to (dd/MMM/yyyy hh:mm:ss a) format :
- This example converts LocalDateTime in default (dd/MMM/yyyy hh:mm:ss a) format to (dd-MM-yyyy hh:mm:ss.nnn a) format
FormatLocalDateTimeExample4.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample4 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd/MMM/yyyy hh:mm:ss a");
// 3. LocalDateTime to (dd/MMM/yyyy hh:mm:ss a) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (dd/MMM/yyyy hh:mm:ss a) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:37:00.697634500
LocalDateTime to (dd/MMM/yyyy hh:mm:ss a) format :-
09/Aug/2022 06:37:00 am
2.5 LocalDateTime to (E, MMM dd yyyy HH:mm) format :
- This example converts LocalDateTime in default (E, MMM dd yyyy HH:mm) format to (dd-MM-yyyy hh:mm:ss.nnn a) format
FormatLocalDateTimeExample5.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample5 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("E, MMM dd yyyy HH:mm");
// 3. LocalDateTime to (E, MMM dd yyyy HH:mm) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (E, MMM dd yyyy HH:mm) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:37:14.005466400
LocalDateTime to (E, MMM dd yyyy HH:mm) format :-
Tue, Aug 09 2022 06:37
2.6 LocalDateTime to (MMM dd, yyyy hh:mm a) format :
- This example converts LocalDateTime in default (MMM dd, yyyy hh:mm a) format to (dd-MM-yyyy hh:mm:ss.nnn a) format
FormatLocalDateTimeExample6.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample6 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("MMM dd, yyyy hh:mm a");
// 3. LocalDateTime to (MMM dd, yyyy hh:mm a) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (MMM dd, yyyy hh:mm a) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:37:31.110987900
LocalDateTime to (MMM dd, yyyy hh:mm a) format :-
Aug 09, 2022 06:37 am
2.7 LocalDateTime to (MM/dd, yyyy hh:mm a) format :
- This example converts LocalDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn) format to (MM/dd, yyyy hh:mm a) format
FormatLocalDateTimeExample7.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeExample7 {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("MM/dd, yyyy hh:mm a");
// 3. LocalDateTime to (MM/dd, yyyy hh:mm a) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to (MM/dd, yyyy hh:mm a) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:37:42.332360500
LocalDateTime to (MM/dd, yyyy hh:mm a) format :-
08/09, 2022 06:37 am
3. Throws DateTimeParseException for Invalid format :
- If the specified pattern/format in invalid then DateTimeParseException is thrown
- In the below illustration, small-letter ‘m‘ is used for Month which is wrong
- Date notation,
- Capital-letter ‘M‘ should be used for Month
- Small-letter ‘d‘ should be used for Day
- Small-letter ‘y‘ should be used for Year
- Time notation,
- Capital-letter ‘H‘ should be used for 24-hour Hour format
- Small-letter ‘h‘ should be used for 12-hour Hour format
- Small-letter ‘m‘ should be used for Minute
- Small-letter ‘s‘ should be used for Second
- Small-letter ‘n‘ should be used for Nanosecond
FormatLocalDateTimeException.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateTimeException {
public static void main(String[] args) {
// 1. get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern("dd.mmm.yyyy HH:mm:ss.nnn");
// 3. LocalDateTime to (dd.mmm.yyyy HH:mm:ss.nnn) format in String form
String str = localDateTime.format(dateTimeFormatter);
System.out.println("\nLocalDateTime to (dd.mmm.yyyy HH:mm:ss.nnn) format :- \n"
+ str);
}
}
Output:
Current System Date/time is :-
2022-08-09T06:35:33.823909800
Exception in thread "main" java.lang.IllegalArgumentException:
Too many pattern letters: m
at java.base/java.time.format.DateTimeFormatterBuilder
.parseField(DateTimeFormatterBuilder.java:2020)
at java.base/java.time.format.DateTimeFormatterBuilder
.parsePattern(DateTimeFormatterBuilder.java:1804)
at java.base/java.time.format.DateTimeFormatterBuilder
.appendPattern(DateTimeFormatterBuilder.java:1772)
at java.base/java.time.format.DateTimeFormatter
.ofPattern(DateTimeFormatter.java:566)
at in.bench.resources.java8.localdatetime.examples.FormatLocalDateTimeException
.main(FormatLocalDateTimeException.java:18)
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 !!