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

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 “Format LocalDate to String in Java”

Leave a Comment

HowToDoInJava

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