Close

Java Date Time - LocalDate.of() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDate

java.lang.Objectjava.lang.Objectjava.time.LocalDatejava.time.LocalDatejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateChronoLocalDatejava.io.SerializableSerializableLogicBig

Methods:

public static LocalDate of(int year,

Month month,

int dayOfMonth)

public static LocalDate of(int year,

int month,

int dayOfMonth)

These methods return a new LocalDate instance from the specified year, month and dayOfMonth. If provided values are not valid DateTimeException is thrown.


Examples


package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Month;

public class OfExample {

public static void main (String... args) {
LocalDate d = LocalDate.of(1997, Month.AUGUST, 20);
System.out.println(d);
}


}

Output

1997-08-20




package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Month;

public class OfExample3 {

public static void main (String... args) {
LocalDate d = LocalDate.of(1997, Month.FEBRUARY, 29);
System.out.println(d);
}
}

Output

Caused by: java.time.DateTimeException: Invalid date 'February 29' as '1997' is not a leap year
at java.time.LocalDate.create(LocalDate.java:429)
at java.time.LocalDate.of(LocalDate.java:249)
at com.logicbig.example.localdate.OfExample3.main(OfExample3.java:14)
... 6 more




package com.logicbig.example.localdate;

import java.time.LocalDate;

public class OfExample2 {

public static void main (String... args) {
LocalDate d = LocalDate.of(2007, 11, 20);
System.out.println(d);
}

}

Output

2007-11-20




See Also