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


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


MonthDay類的isBefore()方法用於檢查此MonthDay是否在作為參數傳遞的MonthDay之前。此方法返回一個顯示相同值的布爾值。

用法:

public boolean isBefore(MonthDay other)

參數:此方法接受一個參數,另一個與另一個month-day進行比較。


返回值:如果此MonthDay在指定的MonthDay之前,則此方法返回true,否則返回false。

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

// Java program to demonstrate 
// MonthDay.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // create a MonthDay object 
        MonthDay month = MonthDay.parse("--10-12"); 
  
        // create other MonthDay object 
        MonthDay othermonth = MonthDay.parse("--11-12"); 
  
        // apply isBefore() method 
        boolean value = month.isBefore(othermonth); 
  
        // print instance 
        System.out.println("monthday:"
                           + month + " is before monthday:"
                           + othermonth + " = "
                           + value); 
    } 
}
輸出:
monthday:--10-12 is before monthday:--11-12 = true

示例2:

// Java program to demonstrate 
// MonthDay.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // create a MonthDay object 
        MonthDay month = MonthDay.parse("--10-12"); 
  
        // create other MonthDay object 
        MonthDay othermonth = MonthDay.parse("--09-12"); 
  
        // apply isBefore() method 
        boolean value = month.isBefore(othermonth); 
  
        // print instance 
        System.out.println("monthday:"
                           + month + " is before monthday:"
                           + othermonth + " = "
                           + value); 
    } 
}
輸出:
monthday:--10-12 is before monthday:--09-12 = false

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



相關用法


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