📜  Java中的 LocalTime isAfter() 方法及示例

📅  最后修改于: 2022-05-13 01:55:39.141000             🧑  作者: Mango

Java中的 LocalTime isAfter() 方法及示例

LocalTimeisAfter()方法用于检查此 LocalTime 时间线位置是否在作为参数传递的 LocalTime 之后。如果此 LocalTime 时间线位置在作为参数传递的 LocalTime 之后,则该方法将返回 true,否则返回 false。比较基于时刻的时间线位置。

句法:

public boolean isAfter(LocalTime other)

参数:此方法接受一个参数 other,它是要比较的另一个 LocalTime对象。它不应该为空。

返回值:如果这个时间在指定时间之后,该方法返回true,否则返回false。

下面的程序说明了 isAfter() 方法:

方案一:

// 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

方案二:

// 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: Java Java.time.LocalTime)