📜  Java中的 OffsetTime compareTo() 方法及示例

📅  最后修改于: 2022-05-13 01:54:27.789000             🧑  作者: Mango

Java中的 OffsetTime compareTo() 方法及示例

Java中 OffsetTime 类的compareTo()方法将此时间与另一个时间进行比较,如果相等则返回零,或者根据比较结果返回正整数或负整数。

句法:

public int compareTo(OffsetTime other)

参数:此方法接受一个强制参数other ,它指定将与之比较的其他时间。

返回值:返回比较器值。如果另一个日期更小,则返回负值,如果更大,则返回正值

异常:如果传递的另一个日期为空,该函数将抛出NullPointerException

下面的程序说明了compareTo()方法:

程序 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));
    }
}
输出:
time1: 15:30:30+07:00
time1: 15:30:30+07:00
time1 when compared to time2 returns: 0

方案二

// 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));
    }
}
输出:
time1: 15:30:30+07:00
time1: 12:10:30+07:00
time1 when compared to time2 returns: 1

程序 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));
    }
}
输出:
time1: 15:30:30+07:00
time1: 17:10:30+07:00
time1 when compared to time2 returns: -1

参考:https: Java/time/OffsetTime.html#compareTo-java.time.OffsetTime-