java.time.Period.negated() 方法示例



描述

java.time.Period.negated() 方法返回此时间段的一个副本,其中长度取反。

声明

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

public Period negated()

返回值

一个以本时间段为基础、金额取反的新时间段,不为 null。

异常

ArithmeticException − 如果发生算术溢出。

示例

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

package com.tutorialspoint;

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
广告
© . All rights reserved.