Duration compareTo(Duration) method in Java with Examples
The compareTo(Duration) method of Duration Class in java.time package is used to compare this duration with the duration passed as the parameter.
Syntax:
public int compareTo(Duration otherDuration)
Parameters: This method accepts a parameter otherDuration which is the duration to which this duration will be compared to.
Return Value: This method returns an int value where negative value means that otherDuration is larger than this value, zero means otherDuration is equal to this duration, and positive value means this duration is larger than the otherDuration.
Below examples illustrate the Duration.compareTo() method:
Example 1:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
Duration duration1
= Duration.parse( "P2DT3H4M" );
Duration duration2
= Duration.ofDays( 10 );
System.out.println(duration1
.compareTo(duration2));
}
}
|
Example 2:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
Duration duration1
= Duration.ofHours( 5 );
Duration duration2
= Duration.ofHours( 5 );
System.out.println(duration1
.compareTo(duration2));
}
}
|
Example 3:
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
Duration duration1
= Duration.ofDays( 5 );
Duration duration2
= Duration.ofHours( 5 );
System.out.println(duration1
.compareTo(duration2));
}
}
|
Reference: Oracle Doc
Last Updated :
26 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...