s-day01-Period类(日期间隔)

This commit is contained in:
2026-07-30 11:46:47 +08:00
parent db399e6db2
commit 361bef782e
@@ -0,0 +1,22 @@
package com.inmind.jdk8_date05;
import java.time.LocalDate;
import java.time.Period;
/**
* 目标:掌握Period的作用:计算机两个日期相差的年数,月数、天数。
注意:只能两个计算LocalDate对象之间的间隔
*/
public class PeriodDemo13 {
public static void main(String[] args) {
//1.创建两个日期对象
LocalDate start = LocalDate.of(2025, 3, 16);
LocalDate end = LocalDate.of(2026, 7, 30);
//2.调用between方法,获得一个Period对象
Period period = Period.between(start, end);
//3.获取间隔的年数,月数,天数
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}