Java Convert LocalDate to String
Tags: Java LocalDate String Java 8 DateTimeFormatter
In this Java core tutorial we learn how to format a java.time.LocalDate object to a date String in Java programming language with different solutions.
Table of contents
- Format LocalDate to String with a Custom Date Pattern
- Format LocalDate to String in Localized Date Pattern
- Format LocalDate to String using predefined format pattern
- Convert LocalDate to String using toString() method
- More Examples of Format LocalDate using DateTimeFormatter
Format LocalDate to String with a Custom Date Pattern
In Java we can use the DateTimeFormatter class with a custom date format pattern to convert a LocalDate object to String using LocalDate.format() method.
For example, in the following Java example code we format LocalDate to String in format of dd MMMM yyyy
ConvertLocalDateToStringExample1.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertLocalDateToStringExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd MMMM yyyy");
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
18 April 2022
Format LocalDate to String in Localized Date Pattern
With DateTimeFormatter class we can get a locale specific date format in different style using the DateTimeFormatter.ofLocalizedDate() method.
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
Below are the list of format style we can use.
- FormatStyle.FULL to format full text style such as Monday, 18 April 2022
- FormatStyle.LONG to format long text style such as 18 April 2022
- FormatStyle.MEDIUM to format medium text style such as 18 Apr 2022
- FormatStyle.SHORT to format short text style such as 18/4/22
In the following Java code we show how to use DateTimeFormatter with FormatStyle.FULL
ConvertLocalDateToStringExample2.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertLocalDateToStringExample2 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
Monday, 18 April 2022
In the following Java code we show how to use DateTimeFormatter with FormatStyle.LONG
ConvertLocalDateToStringExample3.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertLocalDateToStringExample3 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
18 April 2022
In the following Java code we show how to use DateTimeFormatter with FormatStyle.MEDIUM
ConvertLocalDateToStringExample4.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertLocalDateToStringExample4 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
18 Apr 2022
In the following Java code we show how to use DateTimeFormatter with FormatStyle.SHORT
ConvertLocalDateToStringExample5.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertLocalDateToStringExample5 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
18/4/22
Format LocalDate to String using predefined format pattern
With DateTimeFormatter class to format the LocalDate we also can use some predefined constants.
For example in the following Java program we use DateTimeFormatter.ISO_DATE to format LocalDate object.
ConvertLocalDateToStringExample6.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertLocalDateToStringExample6 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
2022-04-18
In the following Java program we use DateTimeFormatter.ISO_WEEK_DATE to format LocalDate object.
ConvertLocalDateToStringExample7.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertLocalDateToStringExample7 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_WEEK_DATE;
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}
2022-W16-1
Convert LocalDate to String using toString() method
In Java we also can simply convert LocalDate to String using the LocalDate.toString() method as the following Java code.
ConvertLocalDateToStringExample8.java
import java.time.LocalDate;
public class ConvertLocalDateToStringExample8 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
String localDateString = localDate.toString();
System.out.println(localDateString);
}
}
2022-04-18
More Examples of Format LocalDate using DateTimeFormatter
In the following Java program we show more examples of how to use custom format pattern to convert LocalDate to String.
ConvertLocalDateToStringExample9.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertLocalDateToStringExample9 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String string1 = localDate.format(formatter1);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
String string2 = localDate.format(formatter2);
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String string3 = localDate.format(formatter3);
DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("dd-LL-yyyy");
String string4 = localDate.format(formatter4);
DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String string5 = localDate.format(formatter5);
DateTimeFormatter formatter6 = DateTimeFormatter.ofPattern("E, MMM dd yyyy");
String string6 = localDate.format(formatter6);
DateTimeFormatter formatter7 = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy");
String string7 = localDate.format(formatter7);
System.out.println("yyyy/MM/dd: " + string1);
System.out.println("MMMM dd, yyyy: " + string2);
System.out.println("dd-MMM-yyyy: " + string3);
System.out.println("dd-LL-yyyy: " + string4);
System.out.println("dd/MM/yyyy: " + string5);
System.out.println("E, MMM dd yyyy: " + string6);
System.out.println("EEEE, MMM dd yyyy: " + string7);
}
}
yyyy/MM/dd: 2022/04/18
MMMM dd, yyyy: April 18, 2022
dd-MMM-yyyy: 18-Apr-2022
dd-LL-yyyy: 18-04-2022
dd/MM/yyyy: 18/04/2022
E, MMM dd yyyy: Mon, Apr 18 2022
EEEE, MMM dd yyyy: Monday, Apr 18 2022
Happy Coding 😊
Related Articles
Java Convert LocalDate to LocalDateTime
Java Convert LocalDate to OffsetDateTime
Java Convert LocalDate to Epoch Day
Java Convert LocalDate to ZonedDateTime
Java Convert LocalDate to Date
Java Convert LocalDate to Calendar
Java Convert Date to LocalDate