MonthDay isValidYear() 方法在 Java 中


在 Java 的 MonthDay 類中,可以使用 isValidYear() 方法檢查一個年份是否對一個 MonthDay 物件有效。此方法需要一個單個引數,即需要檢查的年份。此外,如果一個年份對一個 MonthDay 物件有效,則返回 true,如果不有效,則返回 false。

演示此操作的程式如下所示

示例

 線上演示

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      MonthDay md = MonthDay.parse("--02-21");
      System.out.println("The MonthDay is: " + md);
      System.out.println("Year 2019 is valid for the MonthDay? " + md.isValidYear(2019));
   }
}

輸出

The MonthDay is: --02-21
Year 2019 is valid for the MonthDay? true

現在讓我們理解一下上面的程式。

首先,顯示當前 MonthDay 物件。然後,使用 isValidYear() 方法檢查一個年份是否對 MonthDay 物件有效。列印返回值。演示此操作的程式碼段如下:

MonthDay md = MonthDay.parse("--02-21");
System.out.println("The MonthDay is: " + md);
System.out.println("Year 2019 is valid for the MonthDay? " + md.isValidYear(2019));

更新時間:2019 年 7 月 30 日

77 次觀看

開啟你的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.