Java 8 – How to convert LocalDate in different formats ?

In this article, we will learn how to convert LocalDate in different formats using Java 1.8 version or in short how to convert LocalDate into String-form

For String to LocalDate conversion, read Java 8 – How to convert String to LocalDate ?

1. Convert LocalDate in different formats :

  • We can convert default ISO_LOCAL_DATE format (yyyy-MM-dd) to any other formats using LocalDate.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(“dd.MM.yyyy“)
    2. DateTimeFormatter.ofPattern(“dd-MM-yyyy“)
    3. DateTimeFormatter.ofPattern(“dd/MMM/yyyy“)
    4. DateTimeFormatter.ofPattern(“E, MMM dd yyyy“)
    5. DateTimeFormatter.ofPattern(“MMM dd yyyy“)
    6. DateTimeFormatter.ofPattern(“MM dd, yyyy“)
    7. DateTimeFormatter.ofPattern(“dd MMM, yyyy“)
  • Note: If the specified custom format isn’t in correct form then DateTimeParseException is thrown

2. LocalDate examples in different formats :

2.1 LocalDate to (dd.MM.yyyy) format :

FormatLocalDateExample1.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample1 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");


		// 3. Localdate to (dd.MM.yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.print("\nLocalDate to (dd.MM.yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (dd-MM-yyyy) format :- 
29.07.2022

2.2 LocalDate to (dd-MM-yyyy) format :

FormatLocalDateExample2.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample2 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");


		// 3. Localdate to (dd-MM-yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (dd-MM-yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (dd-MM-yyyy) format :- 
29-07-2022

2.3 LocalDate to (dd/MMM/yyyy) format :

FormatLocalDateExample3.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample3 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy");


		// 3. Localdate to (dd/MMM/yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (dd/MMM/yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (dd/MMM/yyyy) format :- 
29/Jul/2022

2.4 LocalDate to (E, MMM dd yyyy) format :

FormatLocalDateExample4.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample4 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy");


		// 3. Localdate to (E, MMM dd yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (E, MMM dd yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (E, MMM dd yyyy) format :- 
Fri, Jul 29 2022

2.5 LocalDate to (MMM dd yyyy) format :

FormatLocalDateExample5.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample5 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy");


		// 3. Localdate to (MMM dd yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (MMM dd yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (MMM dd yyyy) format :- 
Jul 29 2022

2.6 LocalDate to (MM dd, yyyy) format :

FormatLocalDateExample6.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample6 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM dd, yyyy");


		// 3. Localdate to (MM dd, yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (MM dd, yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (MM dd, yyyy) format :- 
07 29, 2022

2.7 LocalDate to (dd MMM, yyyy) format :

FormatLocalDateExample7.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample7 {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM, yyyy");


		// 3. Localdate to (dd MMM, yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (dd MMM, yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29

LocalDate to (dd MMM, yyyy) format :- 
29 Jul, 2022

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

FormatLocalDateException.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateException {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.m.yyyy");


		// 3. Localdate to (dd MMM, yyyy) format in String form
		String str = localDate.format(dateTimeFormatter);
		System.out.println("\nLocalDate to (dd MMM, yyyy) format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-29
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: 
Unsupported field: MinuteOfHour
	at java.base/java.time.LocalDate.get0(LocalDate.java:709)
	at java.base/java.time.LocalDate.getLong(LocalDate.java:688)
	at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
	at java.base/java.time.format.DateTimeFormatterBuilder$NumberPrinterParser
.format(DateTimeFormatterBuilder.java:2763)
	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.LocalDate.format(LocalDate.java:1813)
	at in.bench.resources.java8.localdate.examples.FormatLocalDateException
.main(FormatLocalDateException.java:20)

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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