The LocalDate class represents the local date in the ISO calendar system. It does not include time and time zone information. A LocalDate can be a date of birth, an official holiday, etc. it is related to a specific day of the year.
The LocalDate class is in the java.time package. Like other classes introduced in Java 8 Date Time API, LocalDate is immutable which means that all computations on LocalDate create a new LocalDate object, so it's safe to be used in multithreading environment.
public final class LocalDate
implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable
2- Static Factory methods
The LocalDate class does not provide any constructor, but it provides static factory methods to create a new object.
static LocalDate now()
static LocalDate now(Clock clock)
static LocalDate now(ZoneId zone)
static LocalDate of(int year, int month, int dayOfMonth)
static LocalDate of(int year, Month month, int dayOfMonth)
static LocalDate ofEpochDay(long epochDay)
static LocalDate ofInstant(Instant instant, ZoneId zone)
static LocalDate ofYearDay(int year, int dayOfYear)
static LocalDate parse(CharSequence text)
static LocalDate parse(CharSequence text, DateTimeFormatter formatter)
Create a LocalDate object representing the current date from the system clock and default time zone (On your computer).
LocalDate localDate = LocalDate.now();
System.out.println("localDate.now(): " + localDate);
Create a LocalDate object representing the current date from a specified clock:
Clock clock = Clock.systemDefaultZone();
LocalDate localDate = LocalDate.now(clock);
System.out.println("localDate.now(clock): " + localDate);
Create a LocalDate object representing the current date from a specified ZoneId:
ZoneId zoneIdDefault = ZoneId.systemDefault();
ZoneId zoneId = ZoneId.of("Asia/Ho_Chi_Minh");
LocalDate localDate = LocalDate.now(zoneId);
System.out.println("localDate: " + localDate);
Create a LocalDate object from the specified year, month, dayOfMonth:
LocalDate localDate1 = LocalDate.of(2000, 5, 20);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.of(2000, Month.MAY, 20);
System.out.println("localDate2: " + localDate2);
Create a LocalDate object from the specified year, dayOfYear:
LocalDate localDate1 = LocalDate.ofYearDay(1995, 1);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.ofYearDay(1995, 51);
System.out.println("localDate2: " + localDate2);
Create a LocalDate object from the specified epochDay:
LocalDate localDate1 = LocalDate.ofEpochDay(0);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.ofEpochDay(1000);
System.out.println("localDate2: " + localDate2);
Create a LocalDate object from Instant and ZoneId objects:
Instant instant = Instant.now();
ZoneId zoneId = ZoneId.systemDefault();
LocalDate localDate = LocalDate.ofInstant(instant, zoneId);
System.out.println("localDate: " + localDate);
Create a LocalDate object from parsing a date formatted text:
LocalDate localDate1 = LocalDate.parse("2011-11-20");
System.out.println("localDate1: " + localDate1);
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate2 = LocalDate.parse("20/11/2020", fmt2);
System.out.println("localDate2: " + localDate2);
3- format(DateTimeFormatter)
Format this LocalDate object with a specified DateTimeFormatter object.
public String format(DateTimeFormatter formatter)
Example:
LocalDate_format_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
DateTimeFormatter fmt1 = DateTimeFormatter.BASIC_ISO_DATE;
System.out.println("BASIC_ISO_DATE: " + localDate.format(fmt1));
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
System.out.println("dd/MM/yyyy: " + localDate.format(fmt2));
4- isBefore(..), isAfter(..)
The isBefore(ChronoLocalDate) method is used to check if this LocalDate object is before another specified object on the timeline.
public boolean isBefore(ChronoLocalDate other)
The isAfter(ChronoLocalDate) method is used to check if this LocalDate object is after another specified object on the timeline.
public boolean isAfter(ChronoLocalDate other)
localDate1 is before localDate2.
LocalDate localDate1 = LocalDate.parse("1990-05-15");
LocalDate localDate2 = LocalDate.parse("2020-12-15");
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println();
System.out.println("localDate1.isBefore(localDate2): " + localDate1.isBefore(localDate2));
System.out.println("localDate1.isAfter(localDate2): " + localDate1.isAfter(localDate2));
Output:
localDate1: 1990-05-15
localDate2: 2020-12-15
localDate1.isBefore(localDate2): true
localDate1.isAfter(localDate2): false
5- lengthOfMonth(), lengthOfYear()
The lengthOfMonth() method returns the number of days in the month represented by this LocalDate object.
The lengthOfYear() method returns the number of days in the year represented by this LocalDate object.
public int lengthOfMonth()
public int lengthOfYear()
Example:
LocalDate_lengthX_ex1.java
package org.codewr.localdate.ex;
import java.time.LocalDate;
public class LocalDate_lengthX_ex1 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
System.out.println("localDate.lengthOfMonth(): " + localDate.lengthOfMonth());
System.out.println("localDate.lengthOfYear(): " + localDate.lengthOfYear());
}
}
The getYear() method returns the year represented by this LocalDate. It is equivalent to calling the get(ChronoField.YEAR) method. If you want to get the year of the era, call the get(ChronoField. YEAR_OF_ERA).
public int getYear()
public int getMonthValue()
public Month getMonth()
public int getDayOfMonth()
public int getDayOfYear()
public DayOfWeek getDayOfWeek()
public IsoEra getEra()
public IsoChronology getChronology()
The other methods getMonthValue(), getMonth(), getDayOfMonth(), getDayOfYear(), getDayOfWeek(), ... are also understood as their names.
Method |
Same As |
getYear() |
localDate.get(ChronoField.YEAR) |
getMonthValue() |
localDate.get(ChronoField.MONTH_OF_YEAR) |
getMonth() |
Month.of(localDate.get(ChronoField.MONTH_OF_YEAR)) |
getDayOfMonth() |
localDate.get(ChronoField.DAY_OF_MONTH) |
getDayOfYear() |
localDate.get(ChronoField.DAY_OF_YEAR) |
getDayOfWeek() |
DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK)) |
getEra() |
localDate.get(ChronoField.ERA) |
getChronology() |
localDate.get(ChronoField.CHRONOLOGY) |
Example:
LocalDate_getX_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
System.out.println("localDate.getEra(): " + localDate.getEra());
System.out.println("localDate.getDayOfYear(): " + localDate.getDayOfYear());
System.out.println("localDate.getDayOfMonth(): " + localDate.getDayOfMonth());
System.out.println("localDate.getYear(): " + localDate.getYear());
System.out.println("localDate.getMonth(): " + localDate.getMonth());
System.out.println("localDate.getMonthValue(): " + localDate.getMonthValue());
System.out.println("localDate.getDayOfWeek(): " + localDate.getDayOfWeek());
Output:
localDate: 2020-05-15
localDate.getEra(): CE
localDate.getDayOfYear(): 136
localDate.getDayOfMonth(): 15
localDate.getYear(): 2020
localDate.getMonth(): MAY
localDate.getMonthValue(): 5
localDate.getDayOfWeek(): FRIDAY
Return the value of the specified field of this LocalDate object as a 32-bit integer.
public int get(TemporalField field)
Note: Some fields may not be supported by LocalDate and an UnsupportedTemporalTypeException will be thrown. To be sure, use the isSupported(TemporalField) method to check if a certain field is supported by LocalDate.
Example:
LocalDate_get_field_ex1.java
LocalDate date = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + date);
System.out.println();
System.out.println("DAY_OF_MONTH: " + date.get(ChronoField.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + date.get(ChronoField.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR: " + date.get(ChronoField.DAY_OF_YEAR));
System.out.println("ERA: " + date.get(ChronoField.ERA));
System.out.println("YEAR: " + date.get(ChronoField.YEAR));
System.out.println("YEAR_OF_ERA: " + date.get(ChronoField.YEAR_OF_ERA));
System.out.println("ALIGNED_DAY_OF_WEEK_IN_MONTH: " + date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
System.out.println("ALIGNED_DAY_OF_WEEK_IN_YEAR: " + date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
System.out.println("ALIGNED_WEEK_OF_MONTH: " + date.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
System.out.println("ALIGNED_WEEK_OF_YEAR: " + date.get(ChronoField.ALIGNED_WEEK_OF_YEAR));
8- getLong(TemporalField)
Return the value of the specified field of this LocalDate object as a 64-bit integer.
public long getLong(TemporalField field)
Note: Some fields may not be supported by LocalDate and an UnsupportedTemporalTypeException will be thrown. To be sure, use the isSupported(TemporalField) method to check if a certain field is supported by LocalDate.
Example:
LocalDate_getLong_field_ex1.java
LocalDate date = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + date);
System.out.println();
System.out.println("DAY_OF_MONTH: " + date.getLong(ChronoField.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + date.getLong(ChronoField.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR: " + date.getLong(ChronoField.DAY_OF_YEAR));
System.out.println("ERA: " + date.getLong(ChronoField.ERA));
System.out.println("YEAR: " + date.getLong(ChronoField.YEAR));
System.out.println("YEAR_OF_ERA: " + date.getLong(ChronoField.YEAR_OF_ERA));
System.out.println("ALIGNED_DAY_OF_WEEK_IN_MONTH: " + date.getLong(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
System.out.println("ALIGNED_DAY_OF_WEEK_IN_YEAR: " + date.getLong(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
System.out.println("ALIGNED_WEEK_OF_MONTH: " + date.getLong(ChronoField.ALIGNED_WEEK_OF_MONTH));
System.out.println("ALIGNED_WEEK_OF_YEAR: " + date.getLong(ChronoField.ALIGNED_WEEK_OF_YEAR));
System.out.println("PROLEPTIC_MONTH: " + date.getLong(ChronoField.PROLEPTIC_MONTH));
System.out.println("EPOCH_DAY: " + date.getLong(ChronoField.EPOCH_DAY));
The plusYear(yearsToAdd) method returns a copy of this LocalDate object with the specified amount of years added.
The methods plusMonths(monthsToAdd), plusWeeks(weeksToAdd), plusDays(daysToAdd) are also understood as the names imply.
public LocalDate plusYears(long yearsToAdd)
public LocalDate plusMonths(long monthsToAdd)
public LocalDate plusWeeks(long weeksToAdd)
public LocalDate plusDays(long daysToAdd)
Example:
LocalDate_plusX_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
LocalDate localDate1 = localDate.plusDays(10);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = localDate.plusDays(-15);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = localDate.plusMonths(3);
System.out.println("localDate3: " + localDate3);
LocalDate localDate4 = localDate.plusYears(1);
System.out.println("localDate4: " + localDate4);
LocalDate localDate5 = localDate.plusWeeks(2);
System.out.println("localDate5: " + localDate5);
Output:
localDate: 2020-05-15
localDate1: 2020-05-25
localDate2: 2020-04-30
localDate3: 2020-08-15
localDate4: 2021-05-15
localDate5: 2020-05-29
Return a copy of this LocalDate object with an added amount of time.
public LocalDate plus(TemporalAmount amountToAdd)
Example:
LocalDate_plus_amount_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
TemporalAmount amount1 = Period.ofDays(5);
LocalDate localDate1 = localDate.plus(amount1);
System.out.println("localDate1: " + localDate1);
LocalDate from = LocalDate.of(2020, 5, 10);
LocalDate to = LocalDate.of(2020, 5, 12);
TemporalAmount amount2 = Period.between(from, to);
LocalDate localDate2 = localDate.plus(amount2);
System.out.println("localDate2: " + localDate2
Output:
localDate: 2020-05-15
localDate1: 2020-05-20
localDate2: 2020-05-17
11- plus(long, TemporalUnit)
Return a copy of this LocalDate object with a specified value added in the given unit.
public LocalDate plus(long amountToAdd, TemporalUnit unit)
Example:
LocalDate_plus_unit_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
LocalDate localDate1 = localDate.plus(5, ChronoUnit.DAYS);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = localDate.plus(10, ChronoUnit.MONTHS);
System.out.println("localDate2: " + localDate2);
Output:
localDate: 2020-05-15
localDate1: 2020-05-20
localDate2: 2021-03-15
The minusYears(yearsToSubtract) method returns a copy of this LocalDate object with the specified amount of years subtracted.
The methods minusMonths(monthsToSubtract), minusWeeks(weeksToSubtract), minusDays(daysToSubtract) is also understood as their names.
public LocalDate minusYears(long yearsToSubtract)
public LocalDate minusMonths(long monthsToSubtract)
public LocalDate minusWeeks(long weeksToSubtract)
public LocalDate minusDays(long daysToSubtract)
Example:
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
LocalDate localDate1 = localDate.minusDays(10);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = localDate.minusDays(-10);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = localDate.minusMonths(3);
System.out.println("localDate3: " + localDate3);
LocalDate localDate4 = localDate.minusYears(1);
System.out.println("localDate4: " + localDate4);
LocalDate localDate5 = localDate.minusWeeks(2);
System.out.println("localDate5: " + localDate5);
TemporalAmount amount6 = Period.ofDays(5);
LocalDate localDate6 = localDate.minus(amount6);
System.out.println("localDate6: " + localDate6);
LocalDate from = LocalDate.of(2020, 5, 10);
LocalDate to = LocalDate.of(2020, 5, 12);
TemporalAmount amount7 = Period.between(from, to);
LocalDate localDate7 = localDate.minus(amount7);
System.out.println("localDate7: " + localDate7);
TemporalUnit unit = ChronoUnit.DAYS;
LocalDate localDate8 = localDate.minus(3, unit);
System.out.println("localDate8: " + localDate8);
Output:
localDate: 2020-05-15
localDate1: 2020-05-05
localDate2: 2020-05-25
localDate3: 2020-02-15
localDate4: 2019-05-15
localDate5: 2020-05-01
localDate6: 2020-05-10
localDate7: 2020-05-13
localDate8: 2020-05-12
13- minus(TemporalAmount)
Return a copy of this LocalDate object with the amount of time subtracted.
public LocalDate minus(TemporalAmount amountToSubtract)
Example:
LocalDate_minus_amount_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
TemporalAmount amount1 = Period.ofDays(5);
LocalDate localDate1 = localDate.minus(amount1);
System.out.println("localDate1: " + localDate1);
LocalDate from = LocalDate.of(2020, 5, 10);
LocalDate to = LocalDate.of(2020, 5, 12);
TemporalAmount amount2 = Period.between(from, to);
LocalDate localDate2 = localDate.minus(amount2);
System.out.println("localDate2: " + localDate2);
14- minus(long, TemporalUnit)
Return a copy of this LocalDate object with a specified value subtracted in given unit.
public LocalDate minus(long amountToSubtract, TemporalUnit unit)
Example:
LocalDate_minus_unit_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("localDate: " + localDate);
System.out.println();
LocalDate localDate1 = localDate.minus(5, ChronoUnit.DAYS);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = localDate.minus(10, ChronoUnit.MONTHS);
System.out.println("localDate2: " + localDate2);
public LocalDate withYear(int year)
public LocalDate withMonth(int month)
public LocalDate withDayOfMonth(int dayOfMonth)
public LocalDate withDayOfYear(int dayOfYear)
The withYear(year) method returns a copy of this LocalDate object with the year changed. If the day-of-month is not valid for the year, it will be changed to the last valid day of month.
LocalDate_withYear_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-02-29").withYear(2024);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.parse("2020-02-29").withYear(2019);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = LocalDate.parse("2020-02-28").withYear(2019);
System.out.println("localDate3: " + localDate3);
LocalDate localDate4 = LocalDate.parse("2020-02-21").withYear(2019);
System.out.println("localDate4: " + localDate4);
The withMonth(month) method returns a copy of this LocalDate object with the month changed. If the day-of-month is not valid for the year, it will be changed to the last valid day-of-month.
LocalDate_withMonth_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-03-31").withMonth(2);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.parse("2020-03-31").withMonth(2);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = LocalDate.parse("2020-03-29").withMonth(2);
System.out.println("localDate3: " + localDate3);
LocalDate localDate4 = LocalDate.parse("2020-03-21").withMonth(2);
System.out.println("localDate4: " + localDate4);
The withDayOfMonth(dayOfMonth) method returns a copy of this LocalDate object with the day-of-month changed. If the day-of-month is invalid, a DateTimeException is thrown.
LocalDate_withDayOfMonth_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-02-01").withDayOfMonth(15);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.parse("2020-02-01").withDayOfMonth(29);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = LocalDate.parse("2020-02-01").withDayOfMonth(30);
System.out.println("localDate3: " + localDate3);
Output:
localDate1: 2020-02-15
localDate2: 2020-02-29
Exception in thread "main" java.time.DateTimeException: Invalid date 'FEBRUARY 30'
at java.base/java.time.LocalDate.create(LocalDate.java:459)
at java.base/java.time.LocalDate.of(LocalDate.java:271)
at java.base/java.time.LocalDate.withDayOfMonth(LocalDate.java:1124)
at org.codewr.localdate.ex.LocalDate_withDayOfMonth_ex1.main(LocalDate_withDayOfMonth_ex1.java:17)
The withDayOfYear(dayOfYear) method returns a copy of this LocalDate object with the day-of-year changed. If dayOfYear is not valid then a DateTimeException is thrown, valid values from 1 to 365 (Or 366).
LocalDate_withDayOfYear_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-02-01").withDayOfYear(15);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = LocalDate.parse("2020-02-01").withDayOfYear(29);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = LocalDate.parse("2020-02-01").withDayOfYear(35);
System.out.println("localDate3: " + localDate3);
16- with(TemporalAdjuster)
Return a copy of this LocalDate object, with the date data adjusted by the TemporalAdjuster object.
public LocalDate with(TemporalAdjuster adjuster)
Example:
LocalDate_with_adjuster_ex1.java
LocalDate now = LocalDate.now();
System.out.println("Now is: " + now);
System.out.println();
LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("firstDayOfMonth: " + firstDayOfMonth);
LocalDate nextMonday = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println("nextMonday: " + nextMonday);
LocalDate firstDayOfNextYear = now.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("firstDayOfNextYear: " + firstDayOfNextYear);
Output:
Now is: 2021-06-01
firstDayOfMonth: 2021-06-01
nextMonday: 2021-06-07
firstDayOfNextYear: 2022-01-01
Example: Write a custom TemporalAdjuster to find the next Christmas date.
LocalDate_with_adjuster_ex2.java
package org.codewr.localdate.ex;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
public class LocalDate_with_adjuster_ex2 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2020-12-28");
System.out.println("Date : " + localDate);
LocalDate localDate2 = localDate.with(new NextChristmas());
System.out.println("Next Christmas : " + localDate2);
}
}
class NextChristmas implements TemporalAdjuster {
@Override
public Temporal adjustInto(Temporal temporal) {
int month = temporal.get(ChronoField.MONTH_OF_YEAR);
int day = temporal.get(ChronoField.DAY_OF_MONTH);
if(month == 12 && day > 25) {
temporal = temporal.plus(Period.ofYears(1));
}
return temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH, 25);
}
}
Output:
Date : 2020-12-28
Next Christmas : 2021-12-25
17- with(TemporalField, long)
Return a copy of this LocalDate object with the specified field changed to the new value.
public LocalDate with(TemporalField field, long newValue)
In some cases, changing the specified field can cause the resulting date to become invalid, such as changing the month from 31st January to February would make the day-of-month invalid. In cases like this, this problem has been handled by this method. Typically it will choose the previous valid date, which would be the last valid day of February in this example.
Example:
LocalDate_with_field_ex1.java
LocalDate myDate = LocalDate.parse("2021-05-29");
System.out.println("myDate is: " + myDate);
System.out.println("myDate day of week: " + myDate.get(ChronoField.DAY_OF_WEEK));
System.out.println("myDate day of month: " + myDate.get(ChronoField.DAY_OF_MONTH));
System.out.println();
LocalDate localDate1 = myDate.with(ChronoField.DAY_OF_WEEK, 3);
System.out.println("localDate1: " + localDate1);
LocalDate localDate2 = myDate.with(ChronoField.DAY_OF_MONTH, 10);
System.out.println("localDate2: " + localDate2);
LocalDate localDate3 = myDate.with(ChronoField.MONTH_OF_YEAR, 2);
System.out.println("localDate3: " + localDate3);
Output:
myDate is: 2021-05-29
myDate day of week: 6
myDate day of month: 29
localDate1: 2021-05-26
localDate2: 2021-05-10
localDate3: 2021-02-28
Return the range of valid values for the specified field.
public ValueRange range(TemporalField field)
Example:
LocalDate_range_ex1.java
LocalDate localDate = LocalDate.parse("2021-06-18");
System.out.println("LocalDate: " + localDate);
ValueRange range = localDate.range(ChronoField.DAY_OF_MONTH);
System.out.println("Range of DAY_OF_MONTH: " + range);
System.out.println("First day of this month: " + range.getMinimum());
System.out.println("Last day of this month: " + range.getMaximum());
ValueRange range2 = localDate.range(ChronoField.DAY_OF_YEAR);
System.out.println("Range of DAY_OF_YEAR: " + range2);
19- query(TemporalQuery<R>)
Query this LocalDate object with the given TemporalQuery parameter to extract information.
public <R> R query(TemporalQuery<R> query)
Example:
LocalDate_query_ex1.java
package org.codewr.localdate.ex;
import java.time.LocalDate;
import java.time.temporal.TemporalQueries;
public class LocalDate_query_ex1 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2021-05-15");
String value = localDate.query(TemporalQueries.precision()).toString();
System.out.println("Precision value for LocalDate is " + value);
System.out.println("Zone value for LocalDate is " + localDate.query(TemporalQueries.offset()));
}
}
Output:
Precision value for LocalDate is Days
Zone value for LocalDate is null
Check if the year represented in this LocalDate object is a leap year.
public boolean isLeapYear()
Basically, in the ISO calendar system, a year is considered a leap year if it is divisible by 4. However a year divisible by 100 is not a leap year except when it is divisible by 400.
So:
- 1904 is a leap year because it was divisible by 4 and not divisible by 100.
- 1900 is not a leap year because it is divisible by 100 but not by 400.
- 2000 is a leap year because it is divisible by 400.
21- isSupported(TemporalField)
Check if a given TemporalField is supported.
public boolean isSupported(TemporalField field)
Basically, LocalDate supports the following TemporalField(s):
- ChronoField.DAY_OF_WEEK
- ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH
- ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR
- ChronoField.DAY_OF_MONTH
- ChronoField.DAY_OF_YEAR
- ChronoField.EPOCH_DAY
- ChronoField.ALIGNED_WEEK_OF_MONTH
- ChronoField.ALIGNED_WEEK_OF_YEAR
- ChronoField.MONTH_OF_YEAR
- ChronoField.PROLEPTIC_MONTH
- ChronoField.YEAR_OF_ERA
- ChronoField.YEAR
- ChronoField.ERA
Example:
LocalDate_isSupported_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("MINUTE_OF_DAY is supported? " + localDate.isSupported(ChronoField.MINUTE_OF_DAY));
System.out.println("HOUR_OF_DAY is supported? " + localDate.isSupported(ChronoField.HOUR_OF_DAY));
System.out.println("MONTH_OF_YEAR is supported? " + localDate.isSupported(ChronoField.MONTH_OF_YEAR));
System.out.println("DAY_OF_WEEK is supported? " + localDate.isSupported(ChronoField.DAY_OF_WEEK));
22- isSupported(TemporalUnit)
Check if a specified TemporalUnit is supported.
public boolean isSupported(TemporalUnit unit)
Basically, LocalDate supports the following TemporalUnit(s):
- ChronoUnit.DAYS
- ChronoUnit.WEEKS
- ChronoUnit.MONTHS
- ChronoUnit.YEARS
- ChronoUnit.DECADES
- ChronoUnit.CENTURIES
- ChronoUnit.MILLENNIA
- ChronoUnit.ERAS
Example:
LocalDate_isSupported_ex2.java
LocalDate localDate = LocalDate.parse("2020-05-15");
System.out.println("HOURS is supported? " + localDate.isSupported(ChronoUnit.HOURS));
System.out.println("MINUTES is supported? " + localDate.isSupported(ChronoUnit.MINUTES));
System.out.println("CENTURIES is supported? " + localDate.isSupported(ChronoUnit.CENTURIES));
System.out.println("WEEKS is supported? " + localDate.isSupported(ChronoUnit.WEEKS));