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

In this article, we will learn how to format LocalDate in different Format Style provided in Java 1.8 version

1. Format LocalDate in different Format Style :

  • FormatStyle class provides 4 different enum constants for formatting LocalDate, 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 LocalDate as mentioned below,
    1. DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
    2. DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
    3. DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
    4. DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
  • Note: for creating above formats we need DateTimeFormatter

2. LocalDate examples in different Format Style :

2.1 LocalDate to FormatStyle.SHORT format :

  • This format style converts LocalDate in default (yyyy-MM-dd) format to (dd/MM/yy) format

FormatLocalDateStyleShort.java

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

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

public class FormatLocalDateStyleShort {

	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
				.ofLocalizedDate(FormatStyle.SHORT);


		// 3. LocalDate to FormatStyle.SHORT format in String-form
		String str = localDate.format(dateTimeFormatter);
		System.out.print("\nLocalDate to FormatStyle.SHORT format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-30

LocalDate to FormatStyle.SHORT format :- 
30/07/22

2.2 LocalDate to FormatStyle.MEDIUM format :

  • This format style converts LocalDate in default (yyyy-MM-dd) format to (dd-MMM-yyyy) format

FormatLocalDateStyleMedium.java

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

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

public class FormatLocalDateStyleMedium {

	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
				.ofLocalizedDate(FormatStyle.MEDIUM);


		// 3. LocalDate to FormatStyle.MEDIUM format in String-form
		String str = localDate.format(dateTimeFormatter);
		System.out.print("\nLocalDate to FormatStyle.MEDIUM format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-30

LocalDate to FormatStyle.MEDIUM format :- 
30-Jul-2022

2.3 LocalDate to FormatStyle.LONG format :

  • This format style converts LocalDate in default (yyyy-MM-dd) format to (dd MMMM yyyy) format

FormatLocalDateStyleLong.java

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

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

public class FormatLocalDateStyleLong {

	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
				.ofLocalizedDate(FormatStyle.LONG);


		// 3. LocalDate to FormatStyle.LONG format in String-form
		String str = localDate.format(dateTimeFormatter);
		System.out.print("\nLocalDate to FormatStyle.LONG format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-30

LocalDate to FormatStyle.LONG format :- 
30 July 2022

2.4 LocalDate to FormatStyle.FULL format :

  • This format style converts LocalDate in default (yyyy-MM-dd) format to (EEEE, dd MMMM, yyyy) format

FormatLocalDateStyleFull.java

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

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

public class FormatLocalDateStyleFull {

	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
				.ofLocalizedDate(FormatStyle.FULL);


		// 3. LocalDate to FormatStyle.FULL format in String-form
		String str = localDate.format(dateTimeFormatter);
		System.out.print("\nLocalDate to FormatStyle.FULL format :- \n"  + str);
	}
}

Output:

Current System Date is :- 
2022-07-30

LocalDate to FormatStyle.FULL format :- 
Saturday, 30 July, 2022

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to add Year, Month and Day fields to LocalDate ?
Java 8 – How to convert LocalDate in different formats ?