java.time.OffsetDateTime.compareTo(OffsetDateTime other)方法

java.time.OffsetDateTime.compareTo(OffsetDateTime other)方法將此日期時間與另一個日期時間進行比較。

聲明

以下是java.time.OffsetDateTime.compareTo(OffsetDateTime other)方法的聲明。

public int compareTo(OffsetDateTime other)

參數

  • other - 要比較的其他日期時間,而不是null

返回值

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

示例

以下示例顯示了java.time.OffsetDateTime.compareTo(OffsetDateTime other)方法的用法。

package com.zaixian;

import java.time.OffsetDateTime;

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

      OffsetDateTime date = OffsetDateTime.parse("2017-02-03T12:30:30+01:00");
      System.out.println(date);
      OffsetDateTime date1 = OffsetDateTime.parse("2017-03-03T12:30:30+01:00");
      System.out.println(date1);
      System.out.println(date1.compareTo(date));
   }
}

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

2017-02-03T12:30:30+01:00
2017-03-03T12:30:30+01:00
1

上一篇: java.time.OffsetDateTime類 下一篇: java.time.OffsetTime類