java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset)方法

2019-10-16 22:40:59

java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset)方法將此ZoneOffset與指定的ZoneOffset進行比較。

宣告

以下是java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset)方法的宣告。

public int compareTo(ZoneOffset otherZoneOffset)

引數

  • otherZoneOffset - 要比較的其他ZoneOffset,而不是null

返回值

比較器值,如果更小則返回為負值,如果大則返回為正值。

例外

  • NullPointerException - 如果otherZoneOffsetnull

範例

以下範例顯示了java.time.ZoneOffset.compareTo(ZoneOffset otherZoneOffset)方法的用法。

package com.yiibai;

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.");  
   }
}

編譯並執行上面的程式,這將產生以下結果 -

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