Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Duration.plusSeconds() method 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.

plusSeconds() is an instance method of the Duration class that is used to add the specified duration, in seconds, to the Duration object.

The plusSeconds() method is defined in the Duration class. The Duration class is defined in the java.time package.

To import the Duration class, use the following import statement.

import java.time.Duration;

Syntax


public Duration plusSeconds(long secondsToAdd)

Parameters

  • long secondsToAdd: The number of seconds to add. This parameter can be positive or negative.

Return value

The plusSeconds() method returns a new instance of the Duration class with the specified number of seconds added.

Code

In the code below, we add 100 seconds to the baseDuration object with the help of the plusSeconds() method and print the new object to the console.

import java.time.Duration;
public class Main{
public static void main(String[] args) {
Duration baseDuration = Duration.ofMinutes(3);
int secondsToAdd = 100;
Duration newDuration = baseDuration.plusSeconds(secondsToAdd);
System.out.printf("%s + %s seconds = %s", baseDuration, secondsToAdd, newDuration);
System.out.println();
}
}

Note: The output is given in the ISO 8601 format.

  • P represents the duration
  • T represents time
  • M represents minutes
  • S represents seconds

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