计时周期是 Java 中的负()方法,示例

原文:https://www . geesforgeks . org/chronoperiod-is negative-method-in-Java-with-examples/

Java 中计时周期类为负()方法用于检查周期中的 YEAR、MONTH、DAYS 三个是否为负。

语法:

boolean isNegative()

参数:该方法不接受任何参数。

返回值:如果给定期间的三值 YEARs、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 # is negative–