In this article, we will learn how to convert LocalDate in different formats using Java 1.8 version or in short how to convert LocalDate into String-form
For String to LocalDate conversion, read Java 8 – How to convert String to LocalDate ?
1. Convert LocalDate in different formats :
- We can convert default ISO_LOCAL_DATE format (yyyy-MM-dd) to any other formats using LocalDate.format() method by passing DateTimeFormatter as argument with required pattern in String-form
- In below illustration, we are using 7 different custom formats as mentioned below,
- DateTimeFormatter.ofPattern(“dd.MM.yyyy“)
- DateTimeFormatter.ofPattern(“dd-MM-yyyy“)
- DateTimeFormatter.ofPattern(“dd/MMM/yyyy“)
- DateTimeFormatter.ofPattern(“E, MMM dd yyyy“)
- DateTimeFormatter.ofPattern(“MMM dd yyyy“)
- DateTimeFormatter.ofPattern(“MM dd, yyyy“)
- DateTimeFormatter.ofPattern(“dd MMM, yyyy“)
- Note: If the specified custom format isn’t in correct form then DateTimeParseException is thrown
2. LocalDate examples in different formats :
2.1 LocalDate to (dd.MM.yyyy) format :
FormatLocalDateExample1.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample1 {
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.ofPattern("dd.MM.yyyy");
// 3. Localdate to (dd.MM.yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.print("\nLocalDate to (dd.MM.yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (dd-MM-yyyy) format :-
29.07.2022
2.2 LocalDate to (dd-MM-yyyy) format :
FormatLocalDateExample2.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample2 {
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.ofPattern("dd-MM-yyyy");
// 3. Localdate to (dd-MM-yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (dd-MM-yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (dd-MM-yyyy) format :-
29-07-2022
2.3 LocalDate to (dd/MMM/yyyy) format :
FormatLocalDateExample3.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample3 {
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.ofPattern("dd/MMM/yyyy");
// 3. Localdate to (dd/MMM/yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (dd/MMM/yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (dd/MMM/yyyy) format :-
29/Jul/2022
2.4 LocalDate to (E, MMM dd yyyy) format :
FormatLocalDateExample4.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample4 {
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.ofPattern("E, MMM dd yyyy");
// 3. Localdate to (E, MMM dd yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (E, MMM dd yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (E, MMM dd yyyy) format :-
Fri, Jul 29 2022
2.5 LocalDate to (MMM dd yyyy) format :
FormatLocalDateExample5.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample5 {
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.ofPattern("MMM dd yyyy");
// 3. Localdate to (MMM dd yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (MMM dd yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (MMM dd yyyy) format :-
Jul 29 2022
2.6 LocalDate to (MM dd, yyyy) format :
FormatLocalDateExample6.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample6 {
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.ofPattern("MM dd, yyyy");
// 3. Localdate to (MM dd, yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (MM dd, yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (MM dd, yyyy) format :-
07 29, 2022
2.7 LocalDate to (dd MMM, yyyy) format :
FormatLocalDateExample7.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateExample7 {
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.ofPattern("dd MMM, yyyy");
// 3. Localdate to (dd MMM, yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (dd MMM, yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
LocalDate to (dd MMM, yyyy) format :-
29 Jul, 2022
3. Throws DateTimeParseException for Invalid format :
- If the specified pattern/format in invalid then DateTimeParseException is thrown
- In the below illustration, small-letter ‘m‘ is used for Month which is wrong
- Date notation,
- Capital-letter ‘M‘ should be used for Month
- Small-letter ‘d‘ should be used for Day
- Small-letter ‘y‘ should be used for Year
FormatLocalDateException.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatLocalDateException {
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.ofPattern("dd.m.yyyy");
// 3. Localdate to (dd MMM, yyyy) format in String form
String str = localDate.format(dateTimeFormatter);
System.out.println("\nLocalDate to (dd MMM, yyyy) format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-29
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException:
Unsupported field: MinuteOfHour
at java.base/java.time.LocalDate.get0(LocalDate.java:709)
at java.base/java.time.LocalDate.getLong(LocalDate.java:688)
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
at java.base/java.time.format.DateTimeFormatterBuilder$NumberPrinterParser
.format(DateTimeFormatterBuilder.java:2763)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1849)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1823)
at java.base/java.time.LocalDate.format(LocalDate.java:1813)
at in.bench.resources.java8.localdate.examples.FormatLocalDateException
.main(FormatLocalDateException.java:20)
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!