当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ChronoPeriod isNegative()用法及代码示例


Java中的ChronoPeriod类的isNegative()方法用于检查期间中的YEAR,MONTH,DAYS三个时间段中的任何一个是否为负。

用法:

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.*; 
import java.time.chrono.*; 
import java.time.temporal.ChronoUnit; 
  
public class ChronoPeriodDemo { 
  
    // Function to check if any of the three 
    // YEAR, MONTH, DAY is negative 
    static void ifNegative(int year, int months, int days) 
    { 
        ChronoPeriod 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.*; 
import java.time.chrono.*; 
import java.time.temporal.ChronoUnit; 
  
public class ChronoPeriodDemo { 
  
    // Function to check if any of the three 
    // YEAR, MONTH, DAY is negative 
    static void ifNegative(int year, int months, int days) 
    { 
        ChronoPeriod 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/9/docs/api/java/time/chrono/ChronoPeriod.html#isNegative–



相关用法


注:本文由纯净天空筛选整理自srinam大神的英文原创作品 ChronoPeriod isNegative() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。