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


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


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

用法:

public boolean isAfter(LocalTime other)

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


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

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

示例1:

// Java program to demonstrate 
// LocalTime.isAfter() 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 after LocalTime 
        // using isAfter() 
        boolean value = time1.isAfter(time2); 
  
        // print result 
        System.out.println("Is LocalTime1 after LocalTime2: "
                           + value); 
    } 
}
輸出:
LocalTime 1: 19:34:50.630
LocalTime 2: 23:14:00.630
Is LocalTime1 after LocalTime2: false

示例2:

// Java program to demonstrate 
// LocalTime.isAfter() 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 after LocalTime 
        // using isAfter() 
        boolean value = time1.isAfter(time2); 
  
        // print result 
        System.out.println("Is LocalTime1 after LocalTime2: "
                           + value); 
    } 
}
輸出:
LocalTime 1: 23:59:11.980
LocalTime 2: 10:24:53.210
Is LocalTime1 after LocalTime2: true

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



相關用法


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