Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
negated
period

What is Period.negated() 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.

Answers Code

negated() is an instance method of the Period, which is used to negate each unit of the Period instance. The method returns a new instance of the Period class after the negation of the year, month, and day units individually. For example, a period of “1 year, 3 months, and -5 days” will be negated to “-1 year, -3 months and, 5 days”.

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

import java.time.Period;

Syntax


public Period negated()

Parameters

The method takes no parameters.

Return value

This method returns a new Period object with the units negated.

Code

In the code snippet below, we are printing the negated version of the Period object to the console.

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

RELATED TAGS

java
communitycreator
negated
period

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.

Answers Code
Keep Exploring