LocalDate format() API

Java 8 examples to format LocalDate to String in default patterns as well as custom date patterns.

1. Default pattern [yyyy-MM-dd]

If we use the LocalDate.toString() method then it format the date in default format which is yyyy-MM-dd.

LocalDate today = LocalDate.now();
        
System.out.println(today.toString());

Program output.

2019-04-03

2. Custom patterns

To format the local date in any other pattern, we must use LocalDate.format(DateTimeFormatter) method.

2.1. Long, medium, short and full patterns

The DateTimeFormatter.ofLocalizedDate(FormatStyle) supports some most used date patterns which we can directly.

LocalDate today = LocalDate.now();
        
String formattedDate = today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
System.out.println("LONG format: " + formattedDate);

formattedDate = today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("MEDIUM format: " + formattedDate);

formattedDate = today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
System.out.println("SHORT format: " + formattedDate);

formattedDate = today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println("FULL format: " + formattedDate);

Program output.

LONG format: 	April 3, 2019
MEDIUM format: 	Apr 3, 2019
SHORT format: 	4/3/19
FULL format: 	Wednesday, April 3, 2019

2.2. User defined pattern

If we have a date pattern which is not available inbuilt, we can define our own pattern and use it.

LocalDate today = LocalDate.now();

String formattedDate = today.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));

System.out.println(formattedDate);

Program output.

03-Apr-19

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

2 thoughts on “LocalDate format() API”

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.