當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Period isNegative()用法及代碼示例


Java中的Period類的isNegative()方法用於檢查period中的三個YEAR,MONTH,DAYS中的任何一個是否為負。

用法:

public boolean isNegative()

參數:此方法不接受任何參數。


返回值:如果給定時間段的三個YEAR,MONTHS,DAYS中的任何一個為負,則此方法返回True,否則將返回false。

以下程序說明了Java中的isNegative()方法:

程序1

// Java code to show the function isNegative() 
// to check whether any of the three given units 
// YEAR, MONTH, DAY is negative 
  
import java.time.Period; 
import java.time.temporal.ChronoUnit; 
  
public class PeriodDemo { 
  
    // Function to check if any of the three  
    // YEAR, MONTH, DAY is negative 
    static void ifNegative(int year, int months, int days) 
    { 
        Period period = Period.of(year, months, days); 
        System.out.println(period.isNegative()); 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
        int year = -12; 
        int months = 3; 
        int days = 31; 
  
        ifNegative(year, months, days); 
    } 
}
輸出:
true

程序2

// Java code to show the function isNegative() 
// to check whether any of the three given units 
// YEAR, MONTH, DAY is negative 
  
import java.time.Period; 
import java.time.temporal.ChronoUnit; 
  
public class PeriodDemo { 
  
    // Function to check if any of the three 
    // YEAR, MONTH, DAY is negative 
    static void ifNegative(int year, int months, int days) 
    { 
        Period period = Period.of(year, months, days); 
        System.out.println(period.isNegative()); 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
        int year = 12; 
        int months = 3; 
        int days = 31; 
  
        ifNegative(year, months, days); 
    } 
}
輸出:
false

參考: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#isNegative–



相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Period isNegative() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。