Java 8 – How to convert LocalDateTime in different Format Style ?

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
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
    3. FormatStyle.LONG
    4. FormatStyle.FULL
  • In below illustration, we are using above provided in-built formats to format LocalDateTime as listed below,
    1. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
    2. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    3. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    4. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDateTime to ZonedDateTime ?
Java 8 – How to convert LocalDateTime in different formats ?