Format LocalDate to String in Java

Learn to format a LocalDate instance to String using inbuilt patterns as well as custom patterns.

1. Inbuilt Patterns

1.1. Default Pattern -> yyyy-MM-dd

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

LocalDate today = LocalDate.now();

System.out.println(today.toString());

Program output.

2019-04-03

1.2. FormatStyle

FormatStyle is an immutable and thread-safe enumeration of the style of ‘localized’ date formatters. Based on the Locale, each constant may output a different string.

It has 4 constants:

  • FULL – Thursday, 17 February, 2022
  • LONG – 17 February 2022
  • MEDIUM – 17/02/22
  • SHORT – 4/3/19
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: 17 February 2022
MEDIUM format: 17-Feb-2022
SHORT format: 17/02/22
FULL format: Thursday, 17 February, 2022

2. Custom Patterns

If we have a date pattern that is not available inbuilt, we can define our own pattern and use it. To format the localdate in any other custom pattern, we must use LocalDate.format(DateTimeFormatter) method.

LocalDate today = LocalDate.now();

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

System.out.println(formattedDate);

Program output.

02-Feb-22

Happy Learning !!

Sourcecode Download

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial