Duration compareTo(Duration) method in Java with Examples Last Updated : 26 Nov, 2018 Comments Improve Suggest changes Like Article Like Report 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: Java // Java code to illustrate compareTo() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Duration using parse() method Duration duration1 = Duration.parse("P2DT3H4M"); // Duration using ofDays() method Duration duration2 = Duration.ofDays(10); // Compare the durations // using compareTo() method System.out.println(duration1 .compareTo(duration2)); } } Output: -1 Example 2: Java // Java code to illustrate compareTo() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Duration using ofHours() method Duration duration1 = Duration.ofHours(5); // Duration using ofHours() method Duration duration2 = Duration.ofHours(5); // Compare the durations // using compareTo() method System.out.println(duration1 .compareTo(duration2)); } } Output: 0 Example 3: Java // Java code to illustrate compareTo() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Duration using ofDays() method Duration duration1 = Duration.ofDays(5); // Duration using ofHours() method Duration duration2 = Duration.ofHours(5); // Compare the durations // using compareTo() method System.out.println(duration1 .compareTo(duration2)); } } Output: 1 Reference: Oracle Doc Comment More infoAdvertise with us Next Article Duration dividedBy(Duration) method in Java with Examples C code_r Follow Improve Article Tags : Java Java-Functions Java-time package Java-Duration Practice Tags : Java Similar Reads Duration plus(Duration) method in Java with Examples The plus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to 2 min read Duration plus(Duration) method in Java with Examples The plus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to 2 min read Duration dividedBy(Duration) method in Java with Examples The dividedBy(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration divided by the duration passed as the parameter. Syntax: public Duration dividedBy(Duration divisor) Parameters: This method accepts a parameter divisor which is the duration to be 2 min read Duration dividedBy(Duration) method in Java with Examples The dividedBy(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration divided by the duration passed as the parameter. Syntax: public Duration dividedBy(Duration divisor) Parameters: This method accepts a parameter divisor which is the duration to be 2 min read Duration minus(Duration) method in Java with Examples The minus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration subtracted, passed as the parameter.Syntax: public Duration minus(Duration duration) Parameters: This method accepts a parameter duration which is the durat 2 min read Duration equals(Duration) method in Java with Examples The equals(Duration) method of Duration Class in java.time package is used to check whether this duration is equal to the duration passed as the parameter. Syntax: public boolean equals(Duration otherDuration) Parameters: This method accepts a parameter otherDuration which is the duration to which t 2 min read Like