當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。