Java LocalDate Class
LocalDate is an immutable date class introduced in Java 8, belonging to java.time package.
LocalDate represents a date without a time zone (year-month-day), for example "2023-05-15". LocalDate does not contain time or time-zone information and is very suitable for scenarios that only need a date, such as birthdays and holidays.
Main features of LocalDate
1. Immutability
LocalDate instances are immutable; any modification operation returns a new LocalDate object rather than modifying the original object.
2. Thread safety
Due to immutability, LocalDate is thread-safe and can be safely used in multi-threaded environments.
3. ISO-8601 standard
LocalDate follows the ISO-8601 calendar system, which is the international standard for modern date and time representation.
Creating a LocalDate object
1. Get the current date
LocalDate today = LocalDate.now();
System.out.println("今天的日期: " + today);
2. Create with a specific date
LocalDate specificDate = LocalDate.of(2023, Month.MAY, 15);
// 或者
LocalDate specificDate2 = LocalDate.of(2023, 5, 15);
3. Parse from a string
LocalDate parsedDate = LocalDate.parse("2023-05-15");
Common operation methods
1. Get date information
Example
LocalDate date
=
LocalDate.
of
(
2023
,
5
,
15
)
;
int
year
=
date.
getYear
(
)
;
// 2023
Month month
=
date.
getMonth
(
)
;
// MAY
int
day
=
date.
getDayOfMonth
(
)
;
// 15
DayOfWeek dow
=
date.
getDayOfWeek
(
)
;
// MONDAY
int
len
=
date.
lengthOfMonth
(
)
;
// 31 (days in May)
boolean
leap
=
date.
isLeapYear
(
)
;
// false (2023 is not a leap year)
2. Date addition and subtraction
Example
LocalDate tomorrow
=
today.
plusDays
(
1
)
;
LocalDate nextWeek
=
today.
plusWeeks
(
1
)
;
LocalDate nextMonth
=
today.
plusMonths
(
1
)
;
LocalDate nextYear
=
today.
plusYears
(
1
)
;
LocalDate yesterday
=
today.
minusDays
(
1
)
;
3. Date comparison
Example
LocalDate date1
=
LocalDate.
of
(
2023
,
5
,
15
)
;
LocalDate date2
=
LocalDate.
of
(
2023
,
6
,
20
)
;
boolean
isBefore
=
date1.
isBefore
(
date2
)
;
// true
boolean
isAfter
=
date1.
isAfter
(
date2
)
;
// false
boolean
isEqual
=
date1.
isEqual
(
date2
)
;
// false
4. Adjust the date
Example
LocalDate date
=
LocalDate.
of
(
2023
,
5
,
15
)
;
LocalDate firstDayOfMonth
=
date.
withDayOfMonth
(
1
)
;
LocalDate lastDayOfYear
=
date.
with
(
TemporalAdjusters.
lastDayOfYear
(
)
)
;
Formatting and parsing
1. Format the date
Example
LocalDate date
=
LocalDate.
now
(
)
;
DateTimeFormatter formatter
=
DateTimeFormatter.
ofPattern
(
"yyyy/MM/dd"
)
;
String
formattedDate
=
date.
format
(
formatter
)
;
2. Parse a string into a date
Example
String
dateStr
=
"2023/05/15"
;
DateTimeFormatter formatter
=
DateTimeFormatter.
ofPattern
(
"yyyy/MM/dd"
)
;
LocalDate date
=
LocalDate.
parse
(
dateStr, formatter
)
;
Practical Application Examples
1. Calculate the number of days between two dates
Example
LocalDate startDate
=
LocalDate.
of
(
2023
,
5
,
1
)
;
LocalDate endDate
=
LocalDate.
of
(
2023
,
5
,
15
)
;
long
daysBetween
=
ChronoUnit.
DAYS
.
between
(
startDate, endDate
)
;
System
.
out
.
println
(
"Day difference: "
daysBetween
)
;
2. Check whether a date is within a range
Example
LocalDate dateToCheck
=
LocalDate.
of
(
2023
,
5
,
10
)
;
LocalDate startDate
=
LocalDate.
of
(
2023
,
5
,
1
)
;
LocalDate endDate
=
LocalDate.
of
(
2023
,
5
,
31
)
;
if
(
dateToCheck.
isAfter
(
startDate
)
&&
dateToCheck.
isBefore
(
endDate
)
)
{
System
.
out
.
println
(
"Date is within range"
)
;
}
3. Get all dates in a certain month
Example
LocalDate date
=
LocalDate.
of
(
2023
,
5
,
1
)
;
int
daysInMonth
=
date.
lengthOfMonth
(
)
;
for
(
int
i
=
1
;
i
&
lt
;=
daysInMonth
;
i
++
)
{
System
.
out
.
println
(
date.
withDayOfMonth
(
i
)
)
;
}
Comprehensive example
Example
import
java.time.LocalDate
;
import
java.time.Month
;
import
java.time.DayOfWeek
;
import
java.time.format.DateTimeFormatter
;
import
java.time.temporal.ChronoUnit
;
import
java.util.Locale
;
public
class
LocalDateDemo
{
public
static
void
main
(
String
[
]
args
)
{
// 1. Create a LocalDate instance
System
.
out
.
println
(
"=== Creating a LocalDate instance ==="
)
;
LocalDate today
=
LocalDate.
now
(
)
;
LocalDate specificDate
=
LocalDate.
of
(
2023
, Month.
JULY
,
15
)
;
LocalDate parsedDate
=
LocalDate.
parse
(
"2023-08-20"
)
;
System
.
out
.
println
(
"Current date: "
today
)
;
System
.
out
.
println
(
// 2023-05-15 (month 0-11)
specificDate
)
;
System
.
out
.
println
(
"Parsed date: "
parsedDate
)
;
// 2. Get date information
System
.
out
.
println
(
"\n=== Getting date information ==="
)
;
System
.
out
.
println
(
"Year: "
today.
getYear
(
)
)
;
System
.
out
.
println
(
"Month: "
today.
getMonth
(
)
" ("
today.
getMonthValue
(
)
")"
)
;
System
.
out
.
println
(
"Day: "
today.
getDayOfMonth
(
)
)
;
System
.
out
.
println
(
"Weekday: "
today.
getDayOfWeek
(
)
)
;
System
.
out
.
println
(
"Day of year: "
today.
getDayOfYear
(
)
)
;
System
.
out
.
println
(
"Days in month: "
today.
lengthOfMonth
(
)
)
;
System
.
out
.
println
(
"Days in year: "
today.
lengthOfYear
(
)
)
;
System
.
out
.
println
(
"Is leap year: "
today.
isLeapYear
(
)
)
;
// 3. Date calculation
System
.
out
.
println
(
"\n=== Date calculation ==="
)
;
System
.
out
.
println
(
"Plus 5 days: "
today.
plusDays
(
5
)
)
;
System
.
out
.
println
(
"Plus 2 weeks: "
today.
plusWeeks
(
2
)
)
;
System
.
out
.
println
(
"Plus 3 months: "
today.
plusMonths
(
3
)
)
;
System
.
out
.
println
(
"Minus 1 year: "
today.
minusYears
(
1
)
)
;
System
.
out
.
println
(
"First day of next month: "
today.
plusMonths
(
1
)
.
withDayOfMonth
(
1
)
)
;
System
.
out
.
println
(
"Last day of this month: "
today.
withDayOfMonth
(
today.
lengthOfMonth
(
)
)
)
;
// 4. Date comparison
System
.
out
.
println
(
"\n=== Date comparison ==="
)
;
LocalDate date1
=
LocalDate.
of
(
2023
,
6
,
15
)
;
LocalDate date2
=
LocalDate.
of
(
2023
,
6
,
20
)
;
System
.
out
.
println
(
date1
" is "
date2
" before? "
date1.
isBefore
(
date2
)
)
;
System
.
out
.
println
(
date1
" is "
date2
" after? "
date1.
isAfter
(
date2
)
)
;
System
.
out
.
println
(
date1
" equals "
date2
"? "
date1.
isEqual
(
date2
)
)
;
// 5. Calculate date difference
System
.
out
.
println
(
"\n=== Calculate date difference ==="
)
;
LocalDate startDate
=
LocalDate.
of
(
2023
,
1
,
1
)
;
LocalDate endDate
=
LocalDate.
of
(
2023
,
12
,
31
)
;
System
.
out
.
println
(
"Days between two dates: "
ChronoUnit.
DAYS
.
between
(
startDate, endDate
)
)
;
System
.
out
.
println
(
"Months between two dates: "
ChronoUnit.
MONTHS
.
between
(
startDate, endDate
)
)
;
System
.
out
.
println
(
"Years between two dates: "
ChronoUnit.
YEARS
.
between
(
startDate, endDate
)
)
;
// 6. Date formatting
System
.
out
.
println
(
"\n=== Date formatting ==="
)
;
DateTimeFormatter basicFormatter
=
DateTimeFormatter.
ofPattern
(
"yyyy/MM/dd"
)
;
DateTimeFormatter chineseFormatter
=
DateTimeFormatter.
ofPattern
(
"yyyy-MM-dd EEEE"
,
Locale
.
CHINA
)
;
DateTimeFormatter customFormatter
=
DateTimeFormatter.
ofPattern
(
"MMM dd, yyyy"
,
Locale
.
US
)
;
System
.
out
.
println
(
"Basic format: "
today.
format
(
basicFormatter
)
)
;
System
.
out
.
println
(
"Chinese format: "
today.
format
(
chineseFormatter
)
)
;
System
.
out
.
println
(
"English format: "
today.
format
(
customFormatter
)
)
;
// 7. Other utility methods
System
.
out
.
println
(
"\n=== Other utility methods ==="
)
;
System
.
out
.
println
(
"Adjusted to next Monday: "
today.
with
(
DayOfWeek.
MONDAY
)
)
;
System
.
out
.
println
(
"Is weekend: "
(
today.
getDayOfWeek
(
)
==
DayOfWeek.
SATURDAY
||
today.
getDayOfWeek
(
)
==
DayOfWeek.
SUNDAY
)
)
;
// 8. Date range iteration
System
.
out
.
println
(
"\n=== Date range iteration ==="
)
;
System
.
out
.
println
(
"Dates for the next 7 days:"
)
;
today.
datesUntil
(
today.
plusDays
(
7
)
)
.
forEach
(
System
.
out
::
println
)
;
}
}
The output is similar to the following:
=== 创建 LocalDate 实例 ===
当前日期: 2023-06-15
特定日期: 2023-07-15
解析日期: 2023-08-20
=== 获取日期信息 ===
年: 2023
月: JUNE (6)
日: 15
星期: THURSDAY
一年中的第几天: 166
月份天数: 30
年份天数: 365
是否闰年: false
=== 日期计算 ===
加5天: 2023-06-20
加2周: 2023-06-29
加3个月: 2023-09-15
减1年: 2022-06-15
下个月的第一天: 2023-07-01
本月的最后一天: 2023-06-30
=== 日期比较 ===
2023-06-15 在 2023-06-20 之前? true
2023-06-15 在 2023-06-20 之后? false
2023-06-15 等于 2023-06-20? false
=== 计算日期差 ===
两个日期之间的天数: 364
两个日期之间的月数: 11
两个日期之间的年数: 0
=== 日期格式化 ===
基本格式: 2023/06/15
中文格式: 2023年06月15日 星期四
英文格式: Jun 15, 2023
=== 其他实用方法 ===
调整到下个星期一: 2023-06-19
是否是周末: false
=== 日期范围迭代 ===
Dates for the next 7 days:
2023-06-15
2023-06-16
2023-06-17
2023-06-18
2023-06-19
2023-06-20
2023-06-21
Summary
The LocalDate class is an important part of the Java 8 Date and Time API. It provides rich methods for handling date-related operations. Compared with the old Date and Calendar classes, LocalDate is more intuitive, easier to use, and thread-safe. Mastering the use of LocalDate can greatly improve the efficiency of date processing and the readability of code.
The following are its commonly used methods:
Static factory methods
| Method | Description | Example |
|---|---|---|
static LocalDate now() |
Get the current date | LocalDate.now() |
static LocalDate of(int year, int month, int dayOfMonth) |
Create with specified year, month, and day | LocalDate.of(2023, 6, 15) |
static LocalDate of(int year, Month month, int dayOfMonth) |
Create with specified year, month, and day (using the Month enum) | LocalDate.of(2023, Month.JUNE, 15) |
static LocalDate parse(CharSequence text) |
Parse from a string (ISO format: yyyy-MM-dd) | LocalDate.parse("2023-06-15") |
static LocalDate parse(CharSequence text, DateTimeFormatter formatter) |
Parse using a specified format | LocalDate.parse("15/06/2023", DateTimeFormatter.ofPattern("dd/MM/yyyy")) |
Get date information
| Method | Description | Example |
|---|---|---|
int getYear() |
Get the year | 2023 |
Month getMonth() |
Get the month (Month enum) | Month.JUNE |
int getMonthValue() |
Get the month value (1-12) | 6 |
int getDayOfMonth() |
Get the day of the month | 15 |
int getDayOfYear() |
Get the day of the year | 166 |
DayOfWeek getDayOfWeek() |
Get the day of the week (DayOfWeek enum) | DayOfWeek.THURSDAY |
int lengthOfMonth() |
Return the number of days in the month | 30 |
int lengthOfYear() |
Return the number of days in the year (365 or 366) | 365 |
boolean isLeapYear() |
Determine whether it is a leap year | false |
Date operations
| Method | Description | Example |
|---|---|---|
LocalDate plusDays(long days) |
Add days | date.plusDays(5) |
LocalDate plusWeeks(long weeks) |
Add weeks | date.plusWeeks(2) |
LocalDate plusMonths(long months) |
Add months | date.plusMonths(3) |
LocalDate plusYears(long years) |
Add years | date.plusYears(1) |
LocalDate minusDays(long days) |
Subtract days | date.minusDays(5) |
LocalDate minusWeeks(long weeks) |
Subtract weeks | date.minusWeeks(2) |
LocalDate minusMonths(long months) |
Subtract months | date.minusMonths(3) |
LocalDate minusYears(long years) |
Subtract years | date.minusYears(1) |
LocalDate withDayOfMonth(int dayOfMonth) |
Modify the day of the month | date.withDayOfMonth(1) |
LocalDate withDayOfYear(int dayOfYear) |
Modify the day of the year | date.withDayOfYear(100) |
LocalDate withMonth(int month) |
Modify the month | date.withMonth(12) |
LocalDate withYear(int year) |
Modify the year | date.withYear(2024) |
Date Comparison
| Method | Description | Example |
|---|---|---|
boolean isAfter(ChronoLocalDate other) |
Whether it is after a specified date | date1.isAfter(date2) |
boolean isBefore(ChronoLocalDate other) |
Whether it is before a specified date | date1.isBefore(date2) |
boolean isEqual(ChronoLocalDate other) |
Whether it equals a specified date | date1.isEqual(date2) |
long until(ChronoLocalDate endDate, ChronoUnit unit) |
Calculate the amount of time between two dates | date1.until(date2, ChronoUnit.DAYS) |
Formatting and conversion
| Method | Description | Example |
|---|---|---|
String format(DateTimeFormatter formatter) |
Format as a string | date.format(DateTimeFormatter.ISO_DATE) |
String toString() |
Convert to an ISO format string (yyyy-MM-dd) | "2023-06-15" |
long toEpochDay() |
Convert to the number of days since 1970-01-01 | 19531 |
LocalDateTime atTime(int hour, int minute) |
Convert to LocalDateTime | date.atTime(14, 30) |
LocalDateTime atTime(LocalTime time) |
Convert to LocalDateTime | date.atTime(LocalTime.now()) |
Other Utility Methods
| Method | Description | Example |
|---|---|---|
boolean isSupported(ChronoUnit unit) |
Check whether a time unit is supported | date.isSupported(ChronoUnit.DAYS) |
boolean isSupported(ChronoField field) |
Check whether a time field is supported | date.isSupported(ChronoField.HOUR_OF_DAY) |
LocalDate with(TemporalAdjuster adjuster) |
Adjust the date using an adjuster | date.with(TemporalAdjusters.firstDayOfMonth()) |
Java java.nio.file.Files