package J20250808.localDateTime; import java.time.LocalDate; import java.time.Period; /** * @author WanJl * @version 1.0 * @title Demo05_PeriodTest * @description * @create 2025/8/8 */ public class Demo05_PeriodTest { public static void main(String[] args) { LocalDate localDate01 = LocalDate.of(2022, 1, 3); LocalDate localDate02 = LocalDate.of(2028, 4, 25); Period period = Period.between(localDate01, localDate02); System.out.println(period); //P6Y3M22D P是间隔缩写,6Y是六年 、3M是3个月 22D 是22天 //获取间隔这段时间的年数 getYears() System.out.println(period.getYears()); //6 //获取间隔这段时间的月数 getMonths() System.out.println(period.getMonths()); //3 //获取间隔这段时间的天数 getDays() System.out.println(period.getDays()); //22 //获取间隔这段时间的总月数 toTotalMonths() System.out.println(period.toTotalMonths()); //75 } }