Java Period Class
Period class is part of a date-time API introduced in Java 8, located in the java.time package.
The Period class is mainly used to represent the time interval between two dates, in units of years, months, and days. Unlike the Duration class (which represents time-based intervals such as hours, minutes, and seconds), thePeriod focuses on differences at the date level.
Core Features
1. Immutability
Period class is immutable, which means once a Period object is created, its value cannot be modified. Any operation on Period (such as addition or subtraction) will return a new Period instance.
2. Date-based calculations
Period Specifically used for calculations involving years, months, and days, suitable for scenarios such as birthdays and contract durations.
3. Coordination with LocalDate
Period It is typically used together with the LocalDate class is used together to calculate the difference between two dates or to add/subtract a period from a date.
// 2022-01-01 00:00:00 GMT
Creating a Period object
1. Using of() Method
Example
Period period
=
Period.
of
(
1
,
2
,
3
)
;
// 1 year, 2 months, 3 days
2. Using between() The method calculates the interval between two dates
Example
LocalDate startDate
=
LocalDate.
of
(
2020
,
1
,
1
)
;
LocalDate endDate
=
LocalDate.
of
(
2021
,
3
,
4
)
;
Period period
=
Period.
between
(
startDate, endDate
)
;
3. Using parse() The method creates from a string
Example
Period period
=
Period.
parse
(
"P1Y2M3D"
)
;
// ISO-8601 format
Getting the parts of a Period
Example
int
years
=
period.
getYears
(
)
;
// Get the number of years
int
months
=
period.
getMonths
(
)
;
// Get the number of months
int
days
=
period.
getDays
(
)
;
// Get the number of days
Arithmetic operations
1. Addition and subtraction operations
Example
Period added
=
period.
plusYears
(
1
)
.
plusMonths
(
2
)
;
Period subtracted
=
period.
minusDays
(
5
)
;
2. Check whether it is negative
Example
boolean
isNegative
=
period.
isNegative
(
)
;
3. Normalize the Period
Example
Period normalized
=
period.
normalized
(
)
;
// Convert the part of months exceeding 12 into years
Practical Application Examples
Calculate the interval between two dates
Example
LocalDate birthDate
=
LocalDate.
of
(
1990
,
5
,
15
)
;
LocalDate currentDate
=
LocalDate.
now
(
)
;
Period age
=
Period.
between
(
birthDate, currentDate
)
;
System
.
out
.
printf
(
"Age: %d years %d months %d days"
,
age.
getYears
(
)
, age.
getMonths
(
)
, age.
getDays
(
)
)
;
Add a Period to a date
Example
LocalDate today
=
LocalDate.
now
(
)
;
Period twoMonths
=
Period.
ofMonths
(
2
)
;
LocalDate futureDate
=
today.
plus
(
twoMonths
)
;
Check whether the period is zero
Example
Period zeroPeriod
=
Period.
of
(
0
,
0
,
0
)
;
if
(
zeroPeriod.
isZero
(
)
)
{
System
.
out
.
println
(
"This is a zero period"
)
;
}
Comprehensive example
Example
import
java.time.Period
;
import
java.time.LocalDate
;
public
class
PeriodDemo
{
public
static
void
main
(
String
[
]
args
)
{
// Several ways to create a Period object
// 1. Use the of() method
Period period1
=
Period.
of
(
1
,
2
,
15
)
;
// 1 year, 2 months, 15 days
System
.
out
.
println
(
"Period 1: "
period1
)
;
// 2. Use the between() method to calculate the period between two dates
LocalDate startDate
=
LocalDate.
of
(
2020
,
1
,
1
)
;
LocalDate endDate
=
LocalDate.
of
(
2023
,
5
,
15
)
;
Period period2
=
Period.
between
(
startDate, endDate
)
;
System
.
out
.
println
(
"Period between dates: "
period2
)
;
// 3. Use parse() to create from a string
Period period3
=
Period.
parse
(
"P1Y2M15D"
)
;
// ISO-8601 format
System
.
out
.
println
(
"Parsed Period: "
period3
)
;
}
}
The output is:
Period 1: P1Y2M15D
Period between dates: P3Y4M14D
Parsed Period: P1Y2M15D
Notes
- Precision Issues:
PeriodOnly calculates the differences in years, months, and days, ignoring differences in time (hours, minutes, etc.). - Negative Value Handling: If the end date is earlier than the start date,
Period.between()will return a negative value. - Month day-count differences:
Periodcalculations take into account the differing number of days in different months (e.g., February has 28 or 29 days). - ISO-8601 format: When using
parse()method, the string must follow thePnYnMnDformat (e.g., "P1Y2M3D").
Summary
Period class is a powerful tool in the Java date-time API for handling date-based intervals. Through it, developers can conveniently perform date-level calculations of years, months, and days, especially suitable for business scenarios that require date precision. Mastering Period class usage can make your date-handling code more concise and readable.