Java 8 – How to form LocalDate passing Year, Month and Day fields ?

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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to parse LocalDate in String form ?
Java 8 - How to get Year, Month and Day fields from LocalDate ?