In this article, we will learn how to format LocalDate in different Format Style provided in Java 1.8 version
1. Format LocalDate in different Format Style :
- FormatStyle class provides 4 different enum constants for formatting LocalDate, those are
- FormatStyle.SHORT
- FormatStyle.MEDIUM
- FormatStyle.LONG
- FormatStyle.FULL
- In below illustration, we are using above provided in-built formats to format LocalDate as mentioned below,
- DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
- DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
- DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
- DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
- Note: for creating above formats we need DateTimeFormatter
2. LocalDate examples in different Format Style :
2.1 LocalDate to FormatStyle.SHORT format :
- This format style converts LocalDate in default (yyyy-MM-dd) format to (dd/MM/yy) format
FormatLocalDateStyleShort.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateStyleShort {
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
.ofLocalizedDate(FormatStyle.SHORT);
// 3. LocalDate to FormatStyle.SHORT format in String-form
String str = localDate.format(dateTimeFormatter);
System.out.print("\nLocalDate to FormatStyle.SHORT format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-30
LocalDate to FormatStyle.SHORT format :-
30/07/22
2.2 LocalDate to FormatStyle.MEDIUM format :
- This format style converts LocalDate in default (yyyy-MM-dd) format to (dd-MMM-yyyy) format
FormatLocalDateStyleMedium.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateStyleMedium {
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
.ofLocalizedDate(FormatStyle.MEDIUM);
// 3. LocalDate to FormatStyle.MEDIUM format in String-form
String str = localDate.format(dateTimeFormatter);
System.out.print("\nLocalDate to FormatStyle.MEDIUM format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-30
LocalDate to FormatStyle.MEDIUM format :-
30-Jul-2022
2.3 LocalDate to FormatStyle.LONG format :
- This format style converts LocalDate in default (yyyy-MM-dd) format to (dd MMMM yyyy) format
FormatLocalDateStyleLong.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateStyleLong {
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
.ofLocalizedDate(FormatStyle.LONG);
// 3. LocalDate to FormatStyle.LONG format in String-form
String str = localDate.format(dateTimeFormatter);
System.out.print("\nLocalDate to FormatStyle.LONG format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-30
LocalDate to FormatStyle.LONG format :-
30 July 2022
2.4 LocalDate to FormatStyle.FULL format :
- This format style converts LocalDate in default (yyyy-MM-dd) format to (EEEE, dd MMMM, yyyy) format
FormatLocalDateStyleFull.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalDateStyleFull {
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
.ofLocalizedDate(FormatStyle.FULL);
// 3. LocalDate to FormatStyle.FULL format in String-form
String str = localDate.format(dateTimeFormatter);
System.out.print("\nLocalDate to FormatStyle.FULL format :- \n" + str);
}
}
Output:
Current System Date is :-
2022-07-30
LocalDate to FormatStyle.FULL format :-
Saturday, 30 July, 2022
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/8/docs/api/java/time/format/FormatStyle.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 !!