20230630 java.time.Period

发布时间 2023-09-04 10:36:23作者: 流星<。)#)))≦

介绍

  • java.time.Period
  • public final class Period implements ChronoPeriod, Serializable
  • 基于日期的时间量
  • 两个 LocalDate 之间的时长是 Period
  • Period 是一个基于日期的 TemporalAmount 实现,存储年月日。
    • Duration 是基于时间的 TemporalAmount 实现,存储秒和纳秒,但使用其他基于 Duration 的单位(例如分钟、小时和固定的 24 小时制日)提供一些访问
  • 实现 TemporalAmount , 可以获取的时间单位:
    • YEARS
    • MONTHS
    • DAYS
  • 内部存储是
private final int years;

private final int months;

private final int days;

API

常量

  • ZERO : P0D

static

  • ofYears

  • ofMonths

  • ofWeeks

  • ofDays

  • of

  • from

    • Period from(TemporalAmount amount)
  • between

    • Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
  • parse

    • toString
    • 格式:P6Y3M1D 表示 9年3月1天

public

  • toTotalMonths
    • 总月数
  • toString

get

  • getYears
  • getMonths
  • getDays

with

  • withYears
  • withMonths
  • withDays

plus

  • plusYears
  • plusMonths
  • plusDays
  • minusYears
  • minusMonths
  • minusDays