Duration withSeconds(long) method in Java with Examples
Last Updated :
26 Nov, 2018
The withSeconds(long) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified amount of seconds passed as the parameter.
Syntax:
public Duration withSeconds(long amountOfSeconds)
Parameters: This method accepts a parameter amountOfSeconds which is the amount of seconds.
Return Value: This method returns a Duration of the seconds passed as the parameter.
Below examples illustrate the Duration.withSeconds() method:
Example 1:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
long amountOfSeconds = 300 ;
Duration duration
= Duration.parse( "P2DT3H4M" );
System.out.println(duration
.withSeconds(amountOfSeconds));
}
}
|
Example 2:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
long amountOfSeconds = 3000 ;
Duration duration
= Duration.ofHours( 5 );
System.out.println(duration
.withSeconds(amountOfSeconds));
}
}
|
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#withSeconds-long-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...