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


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

Java中OffsetDateTime类的isAfter()方法检查该日期是否在指定的日期时间之后,如果是,则返回true。

用法:

public boolean isAfter(OffsetDateTime other)

参数:此方法接受一个参数other,该参数检查此日期时间是否在另一个日期时间之后。


返回值:如果在另一个日期时间之后,则返回true,否则返回false。

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

示例1:

// Java program to demonstrate the isAfter() method 
  
import java.time.OffsetDateTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Parses the date1 
        OffsetDateTime date1 
            = OffsetDateTime 
                  .parse("2018-12-12T13:30:30+05:00"); 
  
        // Parses the date2 
        OffsetDateTime date2 
            = OffsetDateTime 
                  .parse("2018-12-12T13:30:30+05:00"); 
  
        // Prints both dates 
        System.out.println("Date1: "
                           + date1); 
        System.out.println("Date2: "
                           + date2); 
  
        // Compare both 
        System.out.println("Is Date1 after Date2? "
                           + date1.isAfter(date2)); 
    } 
}
输出:
Date1: 2018-12-12T13:30:30+05:00
Date2: 2018-12-12T13:30:30+05:00
Is Date1 after Date2? false

程序2

// Java program to demonstrate the isAfter() method 
  
import java.time.OffsetDateTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Parses the date1 
        OffsetDateTime date1 
            = OffsetDateTime 
                  .parse("2018-12-12T13:30:30+05:00"); 
  
        // Parses the date2 
        OffsetDateTime date2 
            = OffsetDateTime 
                  .parse("2015-12-12T13:30:30+05:00"); 
  
        // Prints both dates 
        System.out.println("Date1: "
                           + date1); 
        System.out.println("Date2: "
                           + date2); 
  
        // Compare both 
        System.out.println("Is Date1 after Date2? "
                           + date1.isAfter(date2)); 
    } 
}
输出:
Date1: 2018-12-12T13:30:30+05:00
Date2: 2015-12-12T13:30:30+05:00
Is Date1 after Date2? true

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



相关用法


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