java.time.Period.negated()方法

java.time.Period.negated()方法返回此Period的副本,其长度为negated

声明

以下是java.time.Period.negated()方法的声明。

public Period negated()

返回值

一个基于此PeriodPeriod,总数为负值,不为null

异常

  • ArithmeticException - 如果发生数字溢出。

示例

以下示例显示了java.time.Period.negated()方法的用法。

package com.zaixian;

import java.time.Period;

public class PeriodDemo {
   public static void main(String[] args) {

      Period period = Period.ofDays(5);
      System.out.println(period.getDays());
      Period period1 = period.negated();
      System.out.println(period1.getDays());
   }
}

执行上面示例代码,得到以下结果:

5
-5

上一篇: java.time.Period类 下一篇: java.time.Year类