Java 8 – How to convert LocalDateTime in different formats ?

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,
    1. DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss.nnn“)
    2. DateTimeFormatter.ofPattern(“dd-MM-yyyy hh:mm:ss.nnn a“)
    3. DateTimeFormatter.ofPattern(“dd.MM.yyyy HH:mm:ss“)
    4. DateTimeFormatter.ofPattern(“dd/MMM/yyyy hh:mm:ss a“)
    5. DateTimeFormatter.ofPattern(“E, MMM dd yyyy HH:mm“)
    6. DateTimeFormatter.ofPattern(“MMM dd, yyyy hh:mm a“)
    7. 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:

References:

Happy Coding !!
Happy Learning !!

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