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


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


LocalTime类的compareTo()方法用于将此LocalTime对象与作为参数传递的LocalTime进行比较,以检查两个LocalTime是否相等。两个LocalTime之间的比较是基于一天中当地时间的时间轴位置。该方法要返回的值确定如下:

  • 如果此LocalTime大于作为参数传递的LocalTime,则返回一个正值
  • 如果此LocalTime等于作为参数传递的LocalTime,则返回零(0)
  • 如果此LocalTime小于作为参数传递的LocalTime,则返回负值。

用法:

public int compareTo(LocalTime other)

参数:此方法接受单个参数LocalTime,它是要比较的另一个LocalTime,并且不应为null。


返回值:此方法返回int值,并且返回值的确定如下:

  • 如果此LocalTime大于作为参数传递的LocalTime,则返回一个正值
  • 如果此LocalTime等于作为参数传递的LocalTime,则返回零(0)
  • 如果此LocalTime小于作为参数传递的LocalTime,则返回负值。

以下示例程序旨在说明compareTo()方法:

示例1:当此LocalTime较大时

// Java program to demonstrate 
// LocalTime.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Objects 
        LocalTime time1 
            = LocalTime.parse("17:52:49"); 
        LocalTime time2 
            = LocalTime.parse("13:08:00"); 
  
        // apply compareTo() 
        int value = time1.compareTo(time2); 
  
        // print LocalDateTime 
        System.out.println("Int Value:" + value); 
  
        if (value > 0) 
            System.out.println("LocalTime1 is greater"); 
        else if (value == 0) 
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2"); 
        else
            System.out.println("LocalTime2 is greater"); 
    } 
}
输出:
Int Value:1
LocalTime1 is greater

示例2:当传递的LocalTime更大时

// Java program to demonstrate 
// LocalTime.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Objects 
        LocalTime time1 
            = LocalTime.parse("07:12:29"); 
        LocalTime time2 
            = LocalTime.parse("13:08:00"); 
  
        // apply compareTo() 
        int value = time1.compareTo(time2); 
  
        // print LocalDateTime 
        System.out.println("Int Value:" + value); 
  
        if (value > 0) 
            System.out.println("LocalTime1 is greater"); 
        else if (value == 0) 
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2"); 
        else
            System.out.println("LocalTime2 is greater"); 
    } 
}
输出:
Int Value:-1
LocalTime2 is greater

示例3:当两个当地时间相等时

// Java program to demonstrate 
// LocalTime.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Objects 
        LocalTime time1 
            = LocalTime.parse("13:08:00"); 
        LocalTime time2 
            = LocalTime.parse("13:08:00"); 
  
        // apply compareTo() 
        int value = time1.compareTo(time2); 
  
        // print LocalDateTime 
        System.out.println("Int Value:" + value); 
  
        if (value > 0) 
            System.out.println("LocalTime1 is greater"); 
        else if (value == 0) 
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2"); 
        else
            System.out.println("LocalTime2 is greater"); 
    } 
}
输出:
Int Value:0
LocalTime1 is equal to LocalTime2

参考: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#compareTo(java.time.LocalTime))



相关用法


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