Period isNegative() 方法在 Java 中


在 Java 中的 Period 類中的 isNegative() 方法中,可以檢查 Period 中的天、月和年是否為負。

此方法不需要引數。此外,如果 Period 中的任意天、月和年為負,則返回 true,如果 Period 中的所有天、月和年均為正,則返回 false。

一個展示此問題的方法如下

示例

import java.time.Period;
public class Demo {
   public static void main(String[] args) {
      String period = "P5Y9M4D";
      Period p = Period.parse(period);
      System.out.println("The Period is: " + p);
      System.out.println("The days, months and years in the Period are negative? " + p.isNegative());
   }
}

 即時演示

The Period is: P5Y9M4D
The days, months and years in the Period are negative? false

輸出

現在讓我們理解一下上述程式。

String period = "P5Y9M4D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The days, months and years in the Period are negative? " +
p.isNegative());

開啟你的 事業

完成課程後獲得證書

開始
廣告
© . All rights reserved.