Period(日期间隔,基于日期)

Period 类位于 java.time 包下,用于表示两个日期之间的时间间隔,精确到年、月、日。它是不可变的,线程安全的,主要用于处理日历日期之间的差异。

  

常用方法及示例

创建 Period 对象

Period.of(int years, int months, int days)

直接通过年、月、日三个参数创建 Period 实例。例如:

import java.time.Period;

public class PeriodDemo {
    public static void main(String[] args) {
        // 创建一个表示"2年3个月5天"的间隔
        Period period = Period.of(2, 3, 5);
        System.out.println(period); // 输出: P2Y3M5D(ISO-8601格式)
    }
}

单单位创建方法

通过单一时间单位创建间隔(其他单位默认为 0):

  • Period.ofYears(int years):仅包含年

  • Period.ofMonths(int months):仅包含月

  • Period.ofWeeks(int weeks):按周创建(1 周 = 7 天)

  • Period.ofDays(int days):仅包含日

Period oneYear = Period.ofYears(1);    // P1Y(1年)
Period twoMonths = Period.ofMonths(2); // P2M(2个月)
Period threeWeeks = Period.ofWeeks(3); // P21D(3周=21天)
Period tenDays = Period.ofDays(10);    // P10D(10天)
Period.between(LocalDate startDate, LocalDate endDate)

计算两个 LocalDate 之间的间隔(结果 = 结束日期 - 开始日期)。

import java.time.LocalDate;

// 计算2020年1月1日到2023年3月15日的间隔
LocalDate start = LocalDate.of(2020, 1, 1);
LocalDate end = LocalDate.of(2023, 3, 15);
Period interval = Period.between(start, end);

System.out.println(interval); // 输出: P3Y2M14D(3年2个月14天)
System.out.println("年: " + interval.getYears());   // 3
System.out.println("月: " + interval.getMonths());  // 2
System.out.println("日: " + interval.getDays());    // 14

注意:若结束日期早于开始日期,返回负间隔(可通过 abs() 取绝对值)。

Period.parse(CharSequence text)

解析 ISO-8601 格式的字符串(如 PnYnMnD)为 Period 实例。

Period parsed1 = Period.parse("P1Y6M");  // 1年6个月
Period parsed2 = Period.parse("P30D");   // 30天
Period parsed3 = Period.parse("P-1Y2M"); // -1年2个月(负间隔)

  

获取间隔信息

getYears() / getMonths() / getDays()

分别获取间隔中的年、月、日数值。

Period period = Period.of(2, 3, 5);
System.out.println(period.getYears());  // 2
System.out.println(period.getMonths()); // 3
System.out.println(period.getDays());   // 5
isZero()

判断间隔是否为 "零间隔"(年、月、日均为 0)。

Period zero = Period.of(0, 0, 0);
System.out.println(zero.isZero()); // true

Period nonZero = Period.of(0, 1, 0);
System.out.println(nonZero.isZero()); // false
isNegative()

判断间隔是否为负值(年、月、日中至少一个为负)。

Period negative = Period.of(-1, 2, 3);
System.out.println(negative.isNegative()); // true

Period positive = Period.of(1, -2, 3);
System.out.println(positive.isNegative()); // true(月为负)

  

间隔运算

加减操作

plusYears(int) / plusMonths(int) / plusDays(int):增加年 / 月 / 日

minusYears(int) / minusMonths(int) / minusDays(int):减少年 / 月 / 日

Period original = Period.of(1, 2, 3); // P1Y2M3D

// 增加操作
Period added = original.plusYears(1).plusMonths(1).plusDays(1);
System.out.println(added); // P2Y3M4D

// 减少操作
Period subtracted = original.minusYears(1).minusDays(2);
System.out.println(subtracted); // P0Y2M1D(可简化为 P2M1D)
multipliedBy(int factor)

将间隔的年、月、日分别乘以系数(支持正负系数)。例如:

Period original = Period.of(1, 2, 3);

Period doubled = original.multipliedBy(2);
System.out.println(doubled); // P2Y4M6D

Period negative = original.multipliedBy(-1);
System.out.println(negative); // P-1Y-2M-3D
negated()

返回当前间隔的负值(年、月、日分别取反)。例如:

Period original = Period.of(1, 2, 3);
Period negated = original.negated();
System.out.println(negated); // P-1Y-2M-3D
abs()

返回间隔的绝对值(将负间隔转为正间隔,正间隔不变)。例如:

Period negative = Period.of(-1, 2, -3);
Period absolute = negative.abs();
System.out.println(absolute); // P1Y2M3D(年和日取绝对值)

日期调整方法

addTo(LocalDate date)

将间隔添加到指定日期,返回新的日期(原日期不变)。例如:

LocalDate baseDate = LocalDate.of(2023, 1, 1);
Period period = Period.of(1, 2, 3); // 1年2个月3天

LocalDate newDate = period.addTo(baseDate);
System.out.println(newDate); // 2024-03-04(2023-01-01 + 1年2个月3天)
subtractFrom(LocalDate date)

从指定日期中减去间隔,返回新的日期(原日期不变)。例如:

LocalDate baseDate = LocalDate.of(2023, 1, 1);
Period period = Period.of(1, 2, 3);

LocalDate newDate = period.subtractFrom(baseDate);
System.out.println(newDate); // 2021-10-29(2023-01-01 - 1年2个月3天)

  

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
其他应用
公众号