OffsetTime compareTo() method in Java with examples Last Updated : 13 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The compareTo() method of OffsetTime class in Java compares this time to another time and returns zero if they are equal or a positive or negative integer depending on the comparison result. Syntax: public int compareTo(OffsetTime other) Parameter: This method accepts a single mandatory parameter other which specifies the other time which will be compared with. Return Value: It returns the comparator value. It returns a negative value if the other date is less or a positive value if greater. Exceptions: The function throws NullPointerException if the other date which is passed is null. Below programs illustrate the compareTo() method: Program 1 : Java // Java program to demonstrate the compareTo() method import java.time.OffsetTime; public class GFG { public static void main(String[] args) { // Parses the time OffsetTime time1 = OffsetTime.parse("15:30:30+07:00"); // Parses the time OffsetTime time2 = OffsetTime.parse("15:30:30+07:00"); // gets the offset time1 System.out.println("time1: " + time1); // gets the offset time2 System.out.println("time1: " + time2); System.out.println("time1 when compared" + " to time2 returns: " + time1.compareTo(time2)); } } Output: time1: 15:30:30+07:00 time1: 15:30:30+07:00 time1 when compared to time2 returns: 0 Program 2 : Java // Java program to demonstrate the compareTo() method import java.time.OffsetTime; public class GFG { public static void main(String[] args) { // Parses the time OffsetTime time1 = OffsetTime.parse("15:30:30+07:00"); // Parses the time OffsetTime time2 = OffsetTime.parse("12:10:30+07:00"); // gets the offset time1 System.out.println("time1: " + time1); // gets the offset time2 System.out.println("time1: " + time2); System.out.println("time1 when compared" + " to time2 returns: " + time1.compareTo(time2)); } } Output: time1: 15:30:30+07:00 time1: 12:10:30+07:00 time1 when compared to time2 returns: 1 Program 3: : Java // Java program to demonstrate the compareTo() method import java.time.OffsetTime; public class GFG { public static void main(String[] args) { // Parses the time OffsetTime time1 = OffsetTime.parse("15:30:30+07:00"); // Parses the time OffsetTime time2 = OffsetTime.parse("17:10:30+07:00"); // gets the offset time1 System.out.println("time1: " + time1); // gets the offset time2 System.out.println("time1: " + time2); System.out.println("time1 when compared" + " to time2 returns: " + time1.compareTo(time2)); } } Output: time1: 15:30:30+07:00 time1: 17:10:30+07:00 time1 when compared to time2 returns: -1 Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#compareTo-java.time.OffsetTime- Comment More infoAdvertise with us Next Article OffsetTime compareTo() method in Java with examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-time package Java-OffsetTime Practice Tags : Java Similar Reads OffsetDateTime compareTo() method in Java with examples The compareTo() method of OffsetDateTime class in Java compares this date-time to another date-time.Syntax : public int compareTo(OffsetDateTime other) Parameter : This method accepts a single parameter other which specifies the other date-time to compare to, not null.Return Value: It returns the co 2 min read Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read UUID compareTo() Method in Java with Examples The compareTo() method of UUID class in Java is used to compare one UUID value with another specified UUID. It returns -1 if this UUID is less than the value, 0 if this UUID is equal to the value, and 1 if this UUID is greater than the value. Syntax: UUID_1.compareTo(UUID_2) Parameters: The method t 2 min read ShortBuffer compareTo method in Java with Examples The compareTo() method of java.nio.ShortBuffer class is used to compare one buffer to another. Two short buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of short 4 min read Path compareTo() method in Java with Examples The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di 3 min read Like