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


Java OffsetDateTime isBefore()用法及代碼示例


Java中的OffsetDateTime類的isBefore()方法檢查該日期是否在指定的日期時間之前,如果是,則返回true。

用法:

public boolean isBefore(OffsetDateTime other)

參數:此方法接受一個參數other,該參數檢查此日期時間是否早於另一個日期時間。


返回值:如果在另一個日期時間之前返回true,否則返回false。

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

示例1:

// Java program to demonstrate the isBefore() 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-11-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("Date1 before Date2? "
                           + date1.isBefore(date2)); 
    } 
}
輸出:
Date1: 2018-11-12T13:30:30+05:00
Date2: 2018-12-12T13:30:30+05:00
Date1 before Date2? true

程序2

// Java program to demonstrate the isBefore() 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("Date1 before Date2? "
                           + date1.isBefore(date2)); 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date2: 2015-12-12T13:30:30+05:00
Date1 before Date2? false

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



相關用法


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