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


Java LocalTime isBefore()用法及代碼示例


LocalTime類的isBefore()方法用於檢查此LocalTime時間軸位置是否在作為參數傳遞的LocalTime之前。如果此LocalTime時間軸位置早於LocalTime作為參數傳遞,則該方法將返回true或false。比較是基於瞬時的時間線位置。

用法:

public boolean isBefore(LocalTime other)

參數:此方法接受一個參數other,該參數是要比較的另一個LocalTime對象。它不能為空。


返回值:如果此時間在指定時間之前,則此方法返回true,否則返回false。

以下示例程序旨在說明isBefore()方法:

示例1:

// Java program to demonstrate 
// LocalTime.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime object 
        LocalTime time1 
            = LocalTime.parse("19:34:50.63"); 
  
        // create other LocalTime 
        LocalTime time2 
            = LocalTime.parse("23:14:00.63"); 
  
        // print instances 
        System.out.println("LocalTime 1: " + time1); 
        System.out.println("LocalTime 2: " + time2); 
  
        // check if LocalTime is before LocalTime 
        // using isBefore() 
        boolean value = time1.isBefore(time2); 
  
        // print result 
        System.out.println("Is LocalTime1 before LocalTime2: "
                           + value); 
    } 
}
輸出:
LocalTime 1: 19:34:50.630
LocalTime 2: 23:14:00.630
Is LocalTime1 before LocalTime2: true

示例2:

// Java program to demonstrate 
// LocalTime.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime object 
        LocalTime time1 
            = LocalTime.parse("23:59:11.98"); 
  
        // create other LocalTime 
        LocalTime time2 
            = LocalTime.parse("10:24:53.21"); 
  
        // print instances 
        System.out.println("LocalTime 1: " + time1); 
        System.out.println("LocalTime 2: " + time2); 
  
        // check if LocalTime is before LocalTime 
        // using isBefore() 
        boolean value = time1.isBefore(time2); 
  
        // print result 
        System.out.println("Is LocalTime1 before LocalTime2: "
                           + value); 
    } 
}
輸出:
LocalTime 1: 23:59:11.980
LocalTime 2: 10:24:53.210
Is LocalTime1 before LocalTime2: false

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



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalTime isBefore() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。