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.