Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Period.isNegative() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

isNegative() is an instance method of the Period class that is used to check if the value of any of the three units (such as years, months, and days) of the Period instance are less than zero.

The isNegative() method is defined in the Period class. The Period class is defined in the java.time package. To import the Period class, use the import statement below:

import java.time.Period;

Syntax


public boolean isNegative()

Parameters

The method accepts no parameters.

Return value

This method returns true if the value of any of the three units is less than zero. Otherwise, it returns false.

Code

In the code below, we check if the Period objects have any unit values that are less than zero.

import java.time.Period;
public class Main{
public static void main(String[] args) {
int numMonths = 10;
int numYears = 5;
int numDays = -13;
Period period = Period.of(numYears, numMonths, numDays);
System.out.printf("isNegative(%s) = %s\n", period, period.isNegative());
numMonths = 10;
numYears = 5;
numDays = 13;
period = Period.of(numYears, numMonths, numDays);
System.out.printf("isNegative(%s) = %s\n", period, period.isNegative());
}
}
An example of the Period.isNegative() function

Explanation

  • Line 1: We import the Period class.
  • Lines 6-10: We test the function on -13 days, 10 months, and 5 years.
  • Lines 12-16: We test the function on 13 days, 10 months, and 5 years.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring