具有示例的java.time.Period类
时间:2020-01-09 10:35:17 来源:igfitidea点击:
java.time.Period类是Java 8中添加的新日期和时间API的一部分,该API表示ISO-8601日历系统中基于日期的时间,例如" 4年,2个月和10天"。
Java Period类以年,月和日为单位对数量或者时间进行建模。这三个字段始终存在,但可以设置为零。
时段类在添加到ZonedDateTime时不考虑夏令时,而是添加一个概念性的日期,以尝试维护本地时间。例如,考虑将夏令时间隔之前的一天的时间增加到晚上18:00。 Period将添加概念日,并在第二天的18:00产生ZonedDateTime。
Period类中的of()方法
我们可以使用以下方法获取Period类的实例:
- of(int years, int months, int days)
- ofDays(int days)
- ofMonths(int months)
- ofWeeks(int weeks)
- ofYears(int years)
public class PeriodExample {
public static void main(String[] args) {
Period period = Period.of(2019, 10, 25);
System.out.println("Period- " + period);
// Year and month as 0
period = Period.ofDays(28);
System.out.println("Period- " + period);
// Year and days as 0
period = Period.ofMonths(11);
System.out.println("Period- " + period);
}
}
输出:
Period- P2019Y10M25D Period- P28D Period- P11M
使用ween()方法查找两个LocalDate之间的差异
Period类中的一种重要方法是between()方法,使用该方法可以获取一个由两个日期之间的年,月和日组成的Period。
Java示例,用于查找两个LocalDate对象之间的年,月和日差异。
public class PeriodExample {
public static void main(String[] args) {
LocalDate ld1 = LocalDate.of(2017, 7, 30);
LocalDate ld2 = LocalDate.now();
System.out.println("From Date- " + ld1);
System.out.println("To Date- " + ld2);
// Find difference
Period period = Period.between(ld1, ld2);
System.out.println("Difference between dates- " + period.getYears() + " Year(s) "
+ period.getMonths()+ " Month(s) " + period.getDays() + " Day(s)");
}
}
输出:
From Date- 2017-07-30 To Date- 2019-11-11 Difference between dates- 2 Year(s) 3 Month(s) 12 Day(s)
从给定日期开始的正负几天,几周,几个月或者一年
使用plus(TemporalAmount amountToAdd)和minus(TemporalAmount amountToSubtract)方法可以从给定的LocalDate中添加或者减去Period。请注意,Period类实现TemporalAmount。
public class PeriodExample {
public static void main(String[] args) {
LocalDate ld = LocalDate.of(2019, 10, 25);
System.out.println("LocalDate- " + ld);
System.out.println("Plus 2 Years " + ld.plus(Period.ofYears(2)));
System.out.println("Plus 3 Months " + ld.plus(Period.ofMonths(3)));
System.out.println("Plus 20 Days " + ld.plus(Period.ofDays(20)));
System.out.println("Plus 3 Weeks " + ld.plus(Period.ofWeeks(3)));
}
}
输出:
LocalDate- 2019-10-25 Plus 2 Years 2021-10-25 Plus 3 Months 2020-01-25 Plus 20 Days 2019-11-14 Plus 3 Weeks 2019-11-15
public class PeriodExample {
public static void main(String[] args) {
LocalDate ld = LocalDate.of(2019, 10, 25);
System.out.println("LocalDate- " + ld);
System.out.println("Minus 2 Years " + ld.minus(Period.ofYears(2)));
System.out.println("Minus 3 Months " + ld.minus(Period.ofMonths(3)));
System.out.println("Minus 20 Days " + ld.minus(Period.ofDays(20)));
System.out.println("Minus 3 Weeks " + ld.minus(Period.ofWeeks(3)));
}
}
输出:
LocalDate- 2019-10-25 Minus 2 Years 2017-10-25 Minus 3 Months 2019-07-25 Minus 20 Days 2019-10-05 Minus 3 Weeks 2019-10-04
将字符串转换为句点– parse()方法
parse(CharSequence text)从文本字符串(例如PnYnMnD)获取一个Period。
在" PnYnMnWnD"格式中," Y"," M"," W"和" D"表示年,月,周和日,均以大写或者小写字母表示。后缀必须按顺序出现。
public class PeriodExample {
public static void main(String[] args) {
Period period = Period.parse("P2Y4M3W4D");
System.out.println("Period- " + period);
period = Period.parse("P1Y7M7D");
System.out.println("Period- " + period);
period = Period.parse("-P1Y2M");
System.out.println("Period- " + period);
}
}
输出:
Period- P2Y4M25D Period- P1Y7M7D Period- P-1Y-2M

