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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。