📌  相关文章
📜  Java中的 ChronoPeriod isNegative() 方法及示例

📅  最后修改于: 2022-05-13 01:55:28.475000             🧑  作者: Mango

Java中的 ChronoPeriod isNegative() 方法及示例

Java中ChronoPeriod 类isNegative() 方法用于检查 period 中的 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

方案二

// 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–