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


Java LocalDateTime isAfter()用法及代码示例


Java中LocalDateTime类的isAfter()方法用于检查作为参数传递的日期是否在此LocalDateTime实例之后。它返回一个显示相同值的布尔值。

用法:

public boolean isAfter(ChronoLocalDateTime otherDate)

参数:此方法接受参数otherDate,该参数指定要与该LocalDateTime比较的其他日期时间。它不能为空。


返回值:该函数返回布尔值,该值显示此日期时间是否晚于指定的日期时间。

以下示例程序旨在说明LocalDateTime.isAfter()方法:

示例1:

// Program to illustrate the isAfter() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
        // Parses the date 
        LocalDateTime dt1 
            = LocalDateTime.parse("2018-11-03T12:45:30"); 
  
        // Prints the date 
        System.out.println(dt1); 
  
        // Parses the date 
        LocalDateTime dt2 
            = LocalDateTime.parse("2016-12-04T12:45:30"); 
  
        // Prints the date 
        System.out.println(dt2); 
  
        // Compares both dates 
        System.out.println(dt1.isAfter(dt2)); 
    } 
}
输出:
2018-11-03T12:45:30
2016-12-04T12:45:30
true

示例2:

// Program to illustrate the isAfter() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
        // Parses the date 
        LocalDateTime dt1 
            = LocalDateTime.parse("2018-11-03T12:45:30"); 
  
        // Prints the date 
        System.out.println(dt1); 
  
        // Parses the date 
        LocalDateTime dt2 
            = LocalDateTime.parse("2019-12-04T12:45:30"); 
  
        // Prints the date 
        System.out.println(dt2); 
  
        // Compares both dates 
        System.out.println(dt1.isAfter(dt2)); 
    } 
}
输出:
2018-11-03T12:45:30
2019-12-04T12:45:30
false

参考: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#isAfter(java.time.chrono.ChronoLocalDateTime)



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LocalDateTime isAfter() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。