Trusted answers to developer questions

What is Duration.toSecondsPart() in Java?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Overview

toSecondsPart() is an instance method of the Duration class. It is used to obtain the number of seconds in the Duration object. The number of seconds is calculated by taking the modulus of the value returned by the toSeconds() method with the value 60. This is based on the traditional 60-second definition of a minute. This method was introduced in Java version 9.

Note: You can read more about the toSeconds() method here.

The toSecondsPart method is defined in the Duration class. The Duration class is defined in the java.time package. To import the Duration class check the following import statement.

import java.time.Duration;

Syntax


public int toSecondsPart()

Parameters

The method has no parameters.

Return value

This method returns the number of seconds part in the Duration object.

Code

import java.time.Duration;
public class Main {
public static void main(String[] args) {
Duration duration = Duration.ofSeconds(143234);
long numOfSecondsPart = duration.toSecondsPart();
System.out.printf("The number of seconds part in %s is %s", duration, numOfSecondsPart);
}
}

Explanation

  • Line 1: We import the Duration class.
  • Line 6: We define a Duration object using the ofSeconds() method.
  • Line 8: We get the number of seconds part in the Duration object using the toSecondsPart() method.
  • Line 10: We print the Duration object and the number of seconds part obtained in line 8.

RELATED TAGS

java
Did you find this helpful?