Java LocalDate by Examples
Tags: Java LocalDate Java 8
In Java the java.time.LocalDate class is used to represent a date without a time-zone in the ISO-8601 calendar system. The java.time.LocalDate class was introduced from Java 8. In this Java core tutorial we learn how to use the java.time.LocalDate class in Java programming language via different example Java programs.
Table of contents
- Create LocalDate object as Current Date using LocalDate.now()
- Create LocalDate object from day month year using LocalDate.of()
- Create LocalDate object from year and day of year using LocalDate.ofYearDay()
- Create LocalDate object from epoch day using LocalDate.ofEpochDay()
- Convert String to LocalDate in Java
- Convert LocalDate to String in Java
- Get Minimum and Maximum of LocalDate
- Get LocalDate Field Values
- Checks if the year is a leap year or not
- LocalDate get length of month
- LocalDate get length of year
- Set day of month for LocalDate using withDayOfMonth() method
- Set day of year for LocalDate using withDayOfYear() method
- Set month for LocalDate using withMonth() method
- Set year for LocalDate using withYear() method
- Add days to LocalDate using plusDays() method
- Add weeks to LocalDate using plusWeeks() method
- Add months to LocalDate using plusMonths() method
- Add years to LocalDate using plusYears() method
- Subtract days from LocalDate using minusDays() method
- Subtract weeks from LocalDate using minusWeeks() method
- Subtract months from LocalDate using minusMonths() method
- Subtract years from LocalDate using minusYears() method
- Check if a LocalDate before specified LocalDate
- Check if a LocalDate after specified LocalDate
- Compare two LocalDate object for Equality
- Compare two LocalDate object for Ordering
Create LocalDate object as Current Date using LocalDate.now()
The LocalDate class provides the LocalDate.now() static method which return the LocalDate object as current date from the system clock in the default time zone as the following Java program.
LocalDateExample1.java
import java.time.LocalDate;
public class LocalDateExample1 {
public static void main(String... args) {
LocalDate now = LocalDate.now();
System.out.println(now);
}
}2022-04-17We can use the LocalDate.now() with a specified time zone as below.
LocalDateExample2.java
import java.time.LocalDate;
import java.time.ZoneId;
public class LocalDateExample2 {
public static void main(String... args) {
ZoneId zoneId = ZoneId.of("Asia/Ho_Chi_Minh");
LocalDate localDate = LocalDate.now(zoneId);
System.out.println("Zone ID: " + zoneId);
System.out.println("LocalDate: " + localDate);
}
}Zone ID: Asia/Ho_Chi_Minh
LocalDate: 2022-04-17Or with a specified clock as following Java code.
LocalDateExample3.java
import java.time.Clock;
import java.time.LocalDate;
public class LocalDateExample3 {
public static void main(String... args) {
Clock clock = Clock.systemUTC();
LocalDate localDate = LocalDate.now(clock);
System.out.println(localDate);
}
}2022-04-17Create LocalDate object from day month year using LocalDate.of()
In Java we can use the LocalDate.of() to create new object of LocalDate from given day month year values as the following Java code.
LocalDateExample4.java
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExample4 {
public static void main(String... args) {
int dayOfMonth = 11;
Month month = Month.JULY;
int monthValue = 7;
int year = 2022;
LocalDate localDate1 = LocalDate.of(year, month, dayOfMonth);
LocalDate localDate2 = LocalDate.of(year, monthValue, dayOfMonth);
System.out.println(localDate1);
System.out.println(localDate2);
}
}2022-07-11
2022-07-11Create LocalDate object from year and day of year using LocalDate.ofYearDay()
In Java we can use the LocalDate.ofYearDay() static method to create a LocalDate object from given year and day of year as the Java example program below.
LocalDateExample5.java
import java.time.LocalDate;
public class LocalDateExample5 {
public static void main(String... args) {
int year = 2022;
int dayOfYear = 45;
LocalDate localDate = LocalDate.ofYearDay(year, dayOfYear);
System.out.println(localDate);
}
}2022-02-14Create LocalDate object from epoch day using LocalDate.ofEpochDay()
With LocalDate class we can use the LocalDate.ofEpochDay() static method to create LocalDate object from a given epoch day count which is an incrementing count of days where day 0 is 1970-01-01.
LocalDateExample6.java
import java.time.LocalDate;
public class LocalDateExample6 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.ofEpochDay(300);
LocalDate localDate2 = LocalDate.ofEpochDay(10000);
LocalDate localDate3 = LocalDate.ofEpochDay(90000);
System.out.println(localDate1);
System.out.println(localDate2);
System.out.println(localDate3);
}
}1970-10-28
1997-05-19
2216-05-31Convert String to LocalDate in Java
In Java we can use the LocalDate.parse() static method to parse a String in format of year month day to LocalDate object. The following Java example to show how to convert a String of date 25 December 2022 to LocalDate object.
LocalDateExample7.java
import java.time.LocalDate;
public class LocalDateExample7 {
public static void main(String... args) {
LocalDate localDate = LocalDate.parse("2022-12-25");
System.out.println(localDate);
}
}2022-12-25With LocalDate.parse() method we can use with DateTimeFormatter to provide a custom date format. For example, we parse a String in format of dd-MM-yyyy as following example Java program.
LocalDateExample8.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample8 {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.parse("25-12-2022", dateTimeFormatter);
System.out.println(localDate);
}
}2022-12-25Convert LocalDate to String in Java
In Java we can use the LocalDate.format() method to format a LocalDate object to a date String with DateTimeFormatter as following Java code.
LocalDateExample9.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample9 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String localDateString = localDate.format(dateTimeFormatter);
System.out.println(localDateString);
}
}17-04-2022Or simply use toString() method to convert a LocalDate object to String as below.
LocalDateExample10.java
import java.time.LocalDate;
public class LocalDateExample10 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
String localDateString = localDate.toString();
System.out.println(localDateString);
}
}2022-04-17Get Minimum and Maximum of LocalDate
In the following Java program we show how to get maximum and minimum values of LocalDate.
LocalDateExample11.java
import java.time.LocalDate;
public class LocalDateExample11 {
public static void main(String... args) {
LocalDate min = LocalDate.MIN;
LocalDate max = LocalDate.MAX;
System.out.println("Minimum LocalDate: " + min);
System.out.println("Maximum LocalDate: " + max);
}
}Minimum LocalDate: -999999999-01-01
Maximum LocalDate: +999999999-12-31Get LocalDate Field Values
In the following Java program we learn how to use these method to get field values of a LocalDate object.
- LocalDate.getYear() to get year value
- LocalDate.getMonthValue() to get month of the year which return int value from 1 to 12
- LocalDate.getMonth() to get month of the year which return Month enum
- LocalDate.getDayOfMonth() to get day of month
- LocalDate.getDayOfYear() to get day of year
- LocalDate.getDayOfWeek() to get day of week which return DayOfWeek enum
LocalDateExample12.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExample12 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int monthValue = localDate.getMonthValue();
Month month = localDate.getMonth();
int dayOfMonth = localDate.getDayOfMonth();
int dayOfYear = localDate.getDayOfYear();
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println("Year: " + year);
System.out.println("Month: " + monthValue);
System.out.println("Month: " + month);
System.out.println("Day of Month: " + dayOfMonth);
System.out.println("Day of Year: " + dayOfYear);
System.out.println("Day of Week: " + dayOfWeek);
}
}Year: 2022
Month: 4
Month: APRIL
Day of Month: 17
Day of Year: 107
Day of Week: SUNDAYChecks if the year is a leap year or not
With LocalDate we can check if the year of LocalDate object is a leap year or not we can use the LocalDate.isLeapYear() method as the following Java code.
LocalDateExample13.java
import java.time.LocalDate;
public class LocalDateExample13 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2020, 5, 10);
LocalDate localDate2 = LocalDate.of(2022, 5, 10);
System.out.println(localDate1.getYear() + " is leap year: " + localDate1.isLeapYear());
System.out.println(localDate2.getYear() + " is leap year: " + localDate2.isLeapYear());
}
}2020 is leap year: true
2022 is leap year: falseLocalDate get length of month
In Java we can get length of month from a given LocalDate object using the LocalDate.lengthOfMonth() method as below.
LocalDateExample14.java
import java.time.LocalDate;
public class LocalDateExample14 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
int lengthOfMonth = localDate.lengthOfMonth();
System.out.println("Length of Month:" + lengthOfMonth);
}
}Length of Month:30LocalDate get length of year
In Java we can get length of year from a given LocalDate object using the LocalDate.lengthOfYear() method as below.
LocalDateExample15.java
import java.time.LocalDate;
public class LocalDateExample15 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
int lengthOfYear = localDate.lengthOfYear();
System.out.println("Length of Year:" + lengthOfYear);
}
}Length of Year:365Set day of month for LocalDate using withDayOfMonth() method
Using LocalDate.withDayOfMonth() method we can create new LocalDate object with updated day of month as following Java code.
LocalDateExample16.java
import java.time.LocalDate;
public class LocalDateExample16 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 20);
localDate = localDate.withDayOfMonth(10);
System.out.println(localDate);
}
}2022-01-10Set day of year for LocalDate using withDayOfYear() method
Using LocalDate.withDayOfYear() method we can create new LocalDate object with updated day of year as following Java code.
LocalDateExample17.java
import java.time.LocalDate;
public class LocalDateExample17 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.withDayOfYear(350);
System.out.println(localDate);
}
}2022-12-16Set month for LocalDate using withMonth() method
Using LocalDate.withMonth() method we can create new LocalDate object with updated month as following Java code.
LocalDateExample18.java
import java.time.LocalDate;
public class LocalDateExample18 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.withMonth(7);
System.out.println(localDate);
}
}2022-07-01Set year for LocalDate using withYear() method
Using LocalDate.withYear() method we can create new LocalDate object with updated year as following Java code.
LocalDateExample19.java
import java.time.LocalDate;
public class LocalDateExample19 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.withYear(2020);
System.out.println(localDate);
}
}2020-01-01Add days to LocalDate using plusDays() method
In Java we can use the LocalDate.plusDays() method to return a copy of LocalDate with number of days be added as the following example Java program.
LocalDateExample20.java
import java.time.LocalDate;
public class LocalDateExample20 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.plusDays(20);
System.out.println(localDate);
}
}2022-01-21Add weeks to LocalDate using plusWeeks() method
In Java we can use the LocalDate.plusWeeks() method to return a copy of LocalDate with number of weeks be added as the following example Java program.
LocalDateExample21.java
import java.time.LocalDate;
public class LocalDateExample21 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.plusWeeks(2);
System.out.println(localDate);
}
}2022-01-15Add months to LocalDate using plusMonths() method
In Java we can use the LocalDate.plusMonths() method to return a copy of LocalDate with number of months be added as the following example Java program.
LocalDateExample22.java
import java.time.LocalDate;
public class LocalDateExample22 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.plusMonths(2);
System.out.println(localDate);
}
}2022-03-01Add years to LocalDate using plusYears() method
In Java we can use the LocalDate.plusYears() method to return a copy of LocalDate with number of years be added as the following example Java program.
LocalDateExample23.java
import java.time.LocalDate;
public class LocalDateExample23 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,1, 1);
localDate = localDate.plusYears(1);
System.out.println(localDate);
}
}2023-01-01Subtract days from LocalDate using minusDays() method
In Java we can use the LocalDate.minusDays() method to return a copy of LocalDate with number of days be subtracted as the following example Java program.
LocalDateExample24.java
import java.time.LocalDate;
public class LocalDateExample24 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,7, 15);
localDate = localDate.minusDays(4);
System.out.println(localDate);
}
}2022-07-11Subtract weeks from LocalDate using minusWeeks() method
In Java we can use the LocalDate.minusWeeks() method to return a copy of LocalDate with number of weeks be subtracted as the following example Java program.
LocalDateExample25.java
import java.time.LocalDate;
public class LocalDateExample25 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,7, 15);
localDate = localDate.minusWeeks(1);
System.out.println(localDate);
}
}2022-07-08Subtract months from LocalDate using minusMonths() method
In Java we can use the LocalDate.minusMonths() method to return a copy of LocalDate with number of months be subtracted as the following example Java program.
LocalDateExample26.java
import java.time.LocalDate;
public class LocalDateExample26 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,7, 15);
localDate = localDate.minusMonths(1);
System.out.println(localDate);
}
}2022-06-15Subtract years from LocalDate using minusYears() method
In Java we can use the LocalDate.minusYears() method to return a copy of LocalDate with number of years be subtracted as the following example Java program.
LocalDateExample27.java
import java.time.LocalDate;
public class LocalDateExample27 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022,7, 11);
localDate = localDate.minusYears(18);
System.out.println(localDate);
}
}2004-07-11Check if a LocalDate before specified LocalDate
The LocalDate class provides the isBefore() method which can be used to check if a LocalDate object is before another LocalDate object or not. We learn how to use the LocalDate.isBefore() method in the following example Java code.
LocalDateExample28.java
import java.time.LocalDate;
public class LocalDateExample28 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 10);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
boolean result1 = localDate1.isBefore(localDate2);
boolean result2 = localDate1.isBefore(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 before localDate2: " + result1);
System.out.println("localDate1 before localDate3: " + result2);
}
}localDate1: 2022-07-11
localDate2: 2022-07-10
localDate3: 2022-07-12
localDate1 before localDate2: false
localDate1 before localDate3: trueCheck if a LocalDate after specified LocalDate
The LocalDate class provides the isAfter() method which can be used to check if a LocalDate object is after another LocalDate object or not. We learn how to use the LocalDate.isAfter() method in the following example Java code.
LocalDateExample29.java
import java.time.LocalDate;
public class LocalDateExample29 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 10);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
boolean result1 = localDate1.isAfter(localDate2);
boolean result2 = localDate1.isAfter(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 after localDate2: " + result1);
System.out.println("localDate1 after localDate3: " + result2);
}
}localDate1: 2022-07-11
localDate2: 2022-07-10
localDate3: 2022-07-12
localDate1 after localDate2: true
localDate1 after localDate3: falseCompare two LocalDate object for Equality
In Java we can use the LocalDate.isEqual() method to compare two LocalDate objects for equality as below.
LocalDateExample30.java
import java.time.LocalDate;
public class LocalDateExample30 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
boolean result1 = localDate1.isEqual(localDate2);
boolean result2 = localDate1.isEqual(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 equal localDate2: " + result1);
System.out.println("localDate1 equal localDate3: " + result2);
}
}localDate1: 2022-07-11
localDate2: 2022-07-11
localDate3: 2022-07-12
localDate1 equal localDate2: true
localDate1 equal localDate3: falseOr we can use the LocalDate.equals() method which return the same result.
LocalDateExample31.java
import java.time.LocalDate;
public class LocalDateExample31 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
boolean result1 = localDate1.equals(localDate2);
boolean result2 = localDate1.equals(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 equal localDate2: " + result1);
System.out.println("localDate1 equal localDate3: " + result2);
}
}localDate1: 2022-07-11
localDate2: 2022-07-11
localDate3: 2022-07-12
localDate1 equal localDate2: true
localDate1 equal localDate3: falseCompare two LocalDate object for Ordering
The LocalDate.compareTo() method to return value of -1, 0, 1 if a LocalDate object is before equal to or after another specified LocalDate object it compare to. We learn how to use LocalDate.compareTo() method in the following example Java program.
LocalDateExample32.java
import java.time.LocalDate;
public class LocalDateExample32 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
int result1 = localDate1.compareTo(localDate2);
int result2 = localDate1.compareTo(localDate3);
int result3 = localDate3.compareTo(localDate1);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 compareTo localDate2: " + result1);
System.out.println("localDate1 compareTo localDate3: " + result2);
System.out.println("localDate3 compareTo localDate1: " + result3);
}
}localDate1: 2022-07-11
localDate2: 2022-07-11
localDate3: 2022-07-12
localDate1 compareTo localDate2: 0
localDate1 compareTo localDate3: -1
localDate3 compareTo localDate1: 1Happy Coding 😊
Related Articles
Java LocalDateTime by Examples