MonthDay.range() 方法在 Java 中


使用 Java 中 MonthDay 類的 range() 方法可以獲取 ChronoField 的值範圍。該方法需要一個引數,即要求其值範圍的 ChronoField,並且它會返回值範圍。

演示該方法的一個程式如下:

示例

 線上演示

import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
public class Main {
   public static void main(String[] args) {
      MonthDay md = MonthDay.parse("--02-21");
      System.out.println("The MonthDay is: " + md);
      ValueRange range = md.range(ChronoField.DAY_OF_MONTH);
      System.out.println("The range of DAY_OF_MONTH is: " + range);
   }
}

輸出

The MonthDay is: --02-21
The range of DAY_OF_MONTH is: 1 - 28/29

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

首先顯示 MonthDay。然後使用 range() 方法獲取特定 ChronoField 的值範圍並顯示。演示此方法的程式碼片段如下:

MonthDay md = MonthDay.parse("--02-21");
System.out.println("The MonthDay is: " + md);
ValueRange range = md.range(ChronoField.DAY_OF_MONTH);
System.out.println("The range of DAY_OF_MONTH is: " + range);

更新於:2019 年 7 月 30 日

79 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.