Thursday, March 12, 2026
HomeLanguagesJavaOffsetTime compareTo() method in Java with examples

OffsetTime compareTo() method in Java with examples

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 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 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 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-

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS