📜  Java中的句点 isNegative() 方法及示例

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

Java中的句点 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

方案二

// 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: Java/time/Period.html#isNegative–