当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java OffsetTime compareTo()用法及代码示例


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

用法:

public int compareTo(OffsetTime other)

参数:此方法接受单个强制性参数other,该参数指定将与之进行比较的其他时间。


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

异常注意:如果传递的其他日期为null,则该函数将引发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

程序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)); 
    } 
}
输出:
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://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#compareTo-java.time.OffsetTime-



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 OffsetTime compareTo() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。