Java 8 LocalDate example.
LocalDate class is a date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day.
Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed.
This class does not store or represent a time or time-zone. Instead, it is a description of the date, as
used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or
time-zone.
This is a value-based class; use of identity-sensitive operations (including reference equality (==),
identity hash code, or synchronization) on instances of LocalDate may have unpredictable results and should be avoided.
The equals method should be used for comparisons.
LocalDate class is immutable and thread-safe.
LocalDateEx |
package com.java2novice.datetime;
import java.time.LocalDate;
import java.time.Month;
public class LocalDateEx {
public static void main(String[] args) {
// get current date
LocalDate currDate = LocalDate.now();
System.out.println("current date: "+currDate);
// create date object with values
LocalDate myBday = LocalDate.of(1982, Month.MARCH, 23);
System.out.println("Created date: "+myBday);
// get date on given year on 'n'th day
LocalDate randomDay = LocalDate.ofYearDay(2015, 100);
System.out.println("Random date: "+randomDay);
}
}
|
|