Demo05_PeriodTest.java 1.0 KB

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