YearMonth类的isSupported(TemporalUnit)方法用于检查YearMonth类是否支持指定的TemporalUnit。实际上,此方法检查是否可以在YearMonth中使用传递的单位对指定的单位应用加法或减法运算。如果为false,则调用plus(long,TemporalUnit)和minus方法将引发异常。
ChronoUnit(Temporalunit)支持的单位是:
- MONTHS
- YEARS
- DECADES
- CENTURIES
- MILLENNIA
- ERAS
所有其他ChronoUnit实例将返回false。
用法:
public boolean isSupported(TemporalUnit unit)
参数:此方法仅接受一个代表要检查的TemporalUnit的参数单元。
返回值:如果此YearMonth支持该单位,则此方法返回布尔值true,否则返回false。
以下示例程序旨在说明isSupported(TemporalUnit)方法:
程序1:
// Java program to demonstrate
// YearMonth.isSupported(TemporalUnit) method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Create a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2017, 8);
// print instance
System.out.println("YearMonth:"
+ thisYearMonth);
// apply isSupported() method
boolean value
= thisYearMonth.isSupported(
ChronoUnit.MONTHS);
// print result
System.out.println("MONTHS unit is supported by YearMonth class:"
+ value);
}
}
输出:
YearMonth:2017-08 MONTHS unit is supported by YearMonth class:true
程序2:
// Java program to demonstrate
// YearMonth.isSupported(TemporalUnit) method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2022, 12);
// print instance
System.out.println("YearMonth:"
+ thisYearMonth);
// apply isSupported() method
boolean value
= thisYearMonth.isSupported(
ChronoUnit.MILLENNIA);
// print result
System.out.println("MILLENNIA unit is supported:"
+ value);
}
}
输出:
YearMonth:2022-12 MILLENNIA unit is supported:true
相关用法
- Java YearMonth with()用法及代码示例
- Java YearMonth until()用法及代码示例
- Java YearMonth plus(TemporalAmount)用法及代码示例
- Java YearMonth query()用法及代码示例
- Java YearMonth withMonth()用法及代码示例
- Java YearMonth isAfter()用法及代码示例
- Java YearMonth withYear()用法及代码示例
- Java YearMonth isBefore()用法及代码示例
- Java YearMonth lengthOfYear()用法及代码示例
- Java YearMonth isValidDay()用法及代码示例
- Java YearMonth range()用法及代码示例
- Java YearMonth isLeapYear()用法及代码示例
- Java YearMonth parse(CharSequence)用法及代码示例
- Java YearMonth plus(long,unit)用法及代码示例
- Java YearMonth isSupported(TemporalField)用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 YearMonth isSupported(TemporalUnit) Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。