java.time.ZoneOffset.compareTo() Method Example



Description

The java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset) method compares this ZoneOffset to the specified ZoneOffset.

Declaration

Following is the declaration for java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset) method.

public int compareTo(ZoneOffset otherZoneOffset)

Parameters

otherZoneOffset − the other ZoneOffset to compare to, not null.

Return Value

the comparator value, negative if less, positive if greater.

Exceptions

NullPointerException − if otherZoneOffset is null.

Example

The following example shows the usage of java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset) method.

package com.tutorialspoint;

import java.time.ZoneOffset;

public class ZoneOffsetDemo {
   public static void main(String[] args) {

      ZoneOffset zoneOffset = ZoneOffset.of("Z");
      System.out.println("ZoneOffset #1: " + zoneOffset);  

      ZoneOffset zoneOffset1 = ZoneOffset.of("+02:00");
      System.out.println("ZoneOffset #2: " + zoneOffset1);  

      int result = zoneOffset.compareTo(zoneOffset1);
      System.out.println(result > 1 ? "ZoneOffset #1 is greater than ZoneOffset #2."
         :"ZoneOffset #2 is greater than ZoneOffset #1.");  
   }
}

Let us compile and run the above program, this will produce the following result −

ZoneOffset #1: Z
ZoneOffset #2: +02:00
ZoneOffset #1 is greater than ZoneOffset #2.
Advertisements