In this article, we will learn how to format LocalDateTime in different Format Style provided in Java 1.8 version
1. Format LocalDateTime in different Format Style :
- FormatStyle class provides 4 different enum constants for formatting LocalDateTime, those are
- FormatStyle.SHORT
- FormatStyle.MEDIUM
- FormatStyle.LONG
- FormatStyle.FULL
- In below illustration, we are using above provided in-built formats to format LocalDateTime as listed below,
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
- Note: for creating above formats we need DateTimeFormatter
2. LocalDateTime examples in different Format Style :
2.1 LocalDateTime to FormatStyle.SHORT format :
- This format style converts LocalDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn) format to (dd/MM/yy, hh:mm a) format ignoring second & nanosecond fields
FormatLocalDateTimeToShortStyle.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateTimeToShortStyle {
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
.ofLocalizedDateTime(FormatStyle.SHORT);
// 3. LocalDateTime to FormatStyle.SHORT format in String-form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to FormatStyle.SHORT format :- \n" + str);
}
}
Output:
Current System Date/time is :-
2022-08-09T07:05:32.317259
LocalDateTime to FormatStyle.SHORT format :-
09/08/22, 7:05 am
2.2 LocalDateTime to FormatStyle.MEDIUM format :
- This format style converts LocalDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn) format to (dd-MMM-yyyy, hh:mm:ss a) format ignoring nanosecond field
FormatLocalDateTimeToMediumStyle.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateTimeToMediumStyle {
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
.ofLocalizedDateTime(FormatStyle.MEDIUM);
// 3. LocalDateTime to FormatStyle.MEDIUM format in String-form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to FormatStyle.MEDIUM format :- \n" + str);
}
}
Output:
Current System Date/time is :-
2022-08-09T07:06:04.989111800
LocalDateTime to FormatStyle.MEDIUM format :-
09-Aug-2022, 7:06:04 am
2.3 LocalDateTime to FormatStyle.LONG format :
- While converting default LocalDateTime format to LONG style format throws runtime exception stating “Zone information is not available“
- So, it should be strictly used when we are dealing with Zone information like ZonedDateTime
FormatLocalDateTimeToLongStyle.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateTimeToLongStyle {
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
.ofLocalizedDateTime(FormatStyle.LONG);
// 3. LocalDateTime to FormatStyle.LONG format in String-form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to FormatStyle.LONG format :- \n" + str);
}
}
Output:
Current System Date/time is :-
2022-08-09T07:06:25.778762800
Exception in thread "main" java.time.DateTimeException:
Unable to extract ZoneId from temporal 2022-08-09T07:06:25.778762800
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:289)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser
.format(DateTimeFormatterBuilder.java:4142)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser
.format(DateTimeFormatterBuilder.java:4844)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1849)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1823)
at java.base/java.time.LocalDateTime.format(LocalDateTime.java:1746)
at in.bench.resources.java8.localdatetime.examples.FormatLocalDateTimeToLongStyle
.main(FormatLocalDateTimeToLongStyle.java:22)
2.4 LocalDateTime to FormatStyle.FULL format :
- This is very much similar like above example which requires Zone information otherwise throws runtime exception stating “Zone information is not available” during conversion from LocalDateTime in default format to FULL style format
- So, it should be strictly used when we are dealing with Zone information like ZonedDateTime
FormatLocalDateTimeToFullStyle.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateTimeToFullStyle {
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
.ofLocalizedDateTime(FormatStyle.FULL);
// 3. LocalDateTime to FormatStyle.FULL format in String-form
String str = localDateTime.format(dateTimeFormatter);
System.out.print("\nLocalDateTime to FormatStyle.FULL format :- \n" + str);
}
}
Output:
Current System Date/time is :-
2022-08-09T07:07:03.968523600
Exception in thread "main" java.time.DateTimeException:
Unable to extract ZoneId from temporal 2022-08-09T07:07:03.968523600
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:289)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser
.format(DateTimeFormatterBuilder.java:4142)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser
.format(DateTimeFormatterBuilder.java:4844)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1849)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1823)
at java.base/java.time.LocalDateTime.format(LocalDateTime.java:1746)
at in.bench.resources.java8.localdatetime.examples.FormatLocalDateTimeToFullStyle
.main(FormatLocalDateTimeToFullStyle.java:22)
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/8/docs/api/java/time/format/FormatStyle.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 !!