# Period
# 介绍
可以用于计算两个LocalDate:对象相差的年数、月数、天数。
# 常用方法
方法名 | 说明 |
---|---|
public static Period between(LocalDate start,LocalDate end) | 传入2个日期对象,得到Period对象 |
public int getYears() | 计算隔几年,并返回 |
public int getMonths() | 计算隔几个月,年返回 |
public int getDays() | 计算隔多少天,并返回 |
# 得到Period对象
import java.time.LocalDate;
import java.time.Period;
public class PeriodTest {
public static void main(String[] args) {
LocalDate of = LocalDate.of(2020, 2, 1);
LocalDate of1 = LocalDate.of(2023, 5, 30);
Period between = Period.between(of, of1);
}
}
# 获取间隔年、月、日
public class PeriodTest {
public static void main(String[] args) {
LocalDate of = LocalDate.of(2020, 2, 1);
LocalDate of1 = LocalDate.of(2023, 5, 30);
Period between = Period.between(of, of1);
int years = between.getYears();
int months = between.getMonths();
int days = between.getDays();
System.out.println("相差年:" + years);
System.out.println("相差月:" + months);
System.out.println("相差日:" + days);
}
}