isSupported()方法是月份ENUM的內置方法,用於檢查是否支持指定的字段。此方法接受字段作為參數,並根據是否支持該字段返回true或false。
用法:
public boolean isSupported(TemporalField field)
參數:此方法接受單個參數字段,將檢查是否支持該參數字段。
返回值:如果支持該字段,則此方法返回布爾值True,否則返回False。
以下示例程序旨在說明上述方法:
程序1:
import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
class monthEnum {
public static void main(String[] args)
{
// Create a month instance
Month month = Month.of(5);
// check if the field is valid
if (month.isSupported(ChronoField.MONTH_OF_YEAR))
System.out.println("This field is supported!");
else
System.out.println("This field is not supported!");
}
}
輸出:
This field is supported!
程序2:
import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
class monthEnum {
public static void main(String[] args)
{
// Create a month instance
Month month = Month.of(5);
// check if the field is valid
if (month.isSupported(ChronoField.DAY_OF_WEEK))
System.out.println("This field is supported!");
else
System.out.println("This field is not supported!");
}
}
輸出:
This field is not supported!
參考: https://www.tutorialspoint.com/javatime/javatime_month_issupported.htm
相關用法
- Java Year atMonth(Month month)用法及代碼示例
- Java LocalDateTime isSupported()用法及代碼示例
- Java ZonedDateTime isSupported()用法及代碼示例
- Java LocalDate isSupported()用法及代碼示例
- Java Instant isSupported()用法及代碼示例
- Java MonthDay isSupported()用法及代碼示例
- Java Charset isSupported()用法及代碼示例
- Java LocalTime isSupported()用法及代碼示例
- Java ChronoLocalDateTime isSupported(TemporalUnit)用法及代碼示例
- Java Year isSupported(TemporalUnit)用法及代碼示例
- Java ChronoZonedDateTime isSupported(TemporalUnit)用法及代碼示例
- Java ChronoZonedDateTime isSupported(TemporalField)用法及代碼示例
- Java ZoneOffset isSupported(TemporalField)用法及代碼示例
- Java YearMonth isSupported(TemporalUnit)用法及代碼示例
- Java Year isSupported(TemporalField)用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Month isSupported() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。