Java LocalDate class
Java LocalDate class is an immutable class that represents Date with a default format of yyyy-mm-dd. It inherits Object class and implements the ChronoLocalDate interface
Java LocalDate class declaration
Let's see the declaration of java.time.LocalDate class.
Methods of Java LocalDate
Method |
Description |
LocalDateTime atTime(int hour, int minute) |
It is used to combine this date with a time to create a LocalDateTime. |
int compareTo(ChronoLocalDate other) |
It is used to compares this date to another date. |
boolean equals(Object obj) |
It is used to check if this date is equal to another date. |
String format(DateTimeFormatter formatter) |
It is used to format this date using the specified formatter. |
int get(TemporalField field) |
It is used to get the value of the specified field from this date as an int. |
boolean isLeapYear() |
It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules. |
LocalDate minusDays(long daysToSubtract) |
It is used to return a copy of this LocalDate with the specified number of days subtracted. |
LocalDate minusMonths(long monthsToSubtract) |
It is used to return a copy of this LocalDate with the specified number of months subtracted. |
static LocalDate now() |
It is used to obtain the current date from the system clock in the default time-zone. |
LocalDate plusDays(long daysToAdd) |
It is used to return a copy of this LocalDate with the specified number of days added. |
LocalDate plusMonths(long monthsToAdd) |
It is used to return a copy of this LocalDate with the specified number of months added. |
LocalDate plusMonths(long monthsToAdd) |
It is used to return a copy of this LocalDate with the specified number of months added. |
int getDayOfMonth() |
It gets the day-of-month field. |
DayOfWeek getDayOfWeek() |
It gets the day-of-week field, which is an enum DayOfWeek. |
int getDayOfYear() |
It gets the day-of-year field. |
Month getMonth() |
It gets the month-of-year field using the Month enum. |
int getMonthValue() |
It gets the month-of-year field from 1 to 12. |
int getYear() |
It gets the year field. |
int lengthOfMonth() |
It returns the length of the month represented by this date. |
int lengthOfYear() |
It returns the length of the year represented by this date. |
static LocalDate ofYearDay(int year, int dayOfYear) |
It obtains an instance of LocalDate from a year and day-of-year. |
static LocalDate parse(CharSequence text) |
It obtains an instance of LocalDate from a text string such as 2007-12-03 |
static LocalDate parse(CharSequence text, DateTimeFormatter formatter) |
It obtains an instance of LocalDate from a text string using a specific formatter. |
Java LocalDate Example
Program to demonstrate methods of LocalDate class such as now(), minusDays(), plusDays().
LocalDateExample1.java
Output:
Today date: 2017-01-13
Yesterday date: 2017-01-12
Tomorrow date: 2017-01-14
Program to demonstrate isLeapYear() method of LocalDate Class.
LocalDateExample2.java
Output:
Program to demonstrate atTime() method of LocalDate class.
LocalDateExample3.java
Output:
Program to demonstrate format() method of LocalDate class
LocalDateExample4.java
Output:
Date1 in string : 2021-09-13
Date2 in string : 2002-05-01
Date3 in string : 2016-11-01
Program to demonstrate parse() method of LocalDate class
LocalDateExample5.java
Output:
String to LocalDate : 2011-09-01
String to LocalDate : 2015-11-20
|