In this article, we will learn how to form LocalDate passing year, month and day fields using different methods in Java 1.8 version
Form LocalDate passing Year, Month and Day:
- LocalDate.of() method returns LocalDate passing year, month and dayOfMonth fields
- There are 2 variants of LocalDate.of() methods –
- LocalDate.of(int year, int month, int dayOfMonth) – month in integer form from 1 (January) to 12 (December)
- LocalDate.of(int year, Month month, int dayOfMonth) – pass month value from Month Enum
- Finally, print LocalDate and different formatted style to the console
FormLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormLocalDate {
public static void main(String[] args) {
// 1. form LocalDate passing year, month & day
LocalDate localDate1 = LocalDate.of(2019, 07, 24);
System.out.println("LocalDate :- " + localDate1);
// 1.1 format LocalDate1
String formattedStr1 = localDate1.format(
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("Formatted LocalDate :- " + formattedStr1);
// 2. form LocalDate passing year, Month enum & day
LocalDate localDate2 = LocalDate.of(2017, Month.DECEMBER, 15);
System.out.println("\nLocalDate :- " + localDate2);
// 2.1 format LocalDate2
String formattedStr2 = localDate2.format(
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println("Formatted LocalDate :- " + formattedStr2);
}
}
Output:
LocalDate :- 2019-07-24
Formatted LocalDate :- 24-Jul-2019
LocalDate :- 2017-12-15
Formatted LocalDate :- Friday, 15 December, 2017
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/Month.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 !!