Java中YearMonth类的now(Clock clock)方法用于从指定的时钟获取当前的year-month。句法:
public static YearMonth now(Clock clock)
参数:此方法接受clock作为代表要使用的时钟的参数。
返回值:此方法返回当前的year-month。
以下示例程序旨在说明Java中YearMonth的now(Clock clock)方法:
程序1:
// Java program to demonstrate
// YearMonth.now(Clock clock) method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// apply now(Clock clock) method
// of YearMonth class
YearMonth result
= YearMonth.now(
Clock.systemUTC());
// print year and month both
System.out.println(
"YearMonth:"
+ result);
}
}
输出:
YearMonth:2020-05
程序2:
// Java program to demonstrate
// YearMonth.now(Clock clock) method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// apply now(Clock clock) method
// of YearMonth class
YearMonth result
= YearMonth.now(
Clock.systemUTC());
// print only year
System.out.println("Year:"
+ result.get(
ChronoField.YEAR));
}
}
输出:
Year:2020
参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#now(java.time.Clock)
相关用法
- Java YearMonth with()用法及代码示例
- Java YearMonth until()用法及代码示例
- Java YearMonth now()用法及代码示例
- Java YearMonth get()用法及代码示例
- Java YearMonth withMonth()用法及代码示例
- Java YearMonth range()用法及代码示例
- Java YearMonth minusMonths()用法及代码示例
- Java YearMonth withYear()用法及代码示例
- Java YearMonth query()用法及代码示例
- Java YearMonth isValidDay()用法及代码示例
- Java YearMonth plusYears()用法及代码示例
- Java YearMonth plus(TemporalAmount)用法及代码示例
- Java YearMonth lengthOfYear()用法及代码示例
- Java YearMonth getLong()用法及代码示例
- Java YearMonth isLeapYear()用法及代码示例
注:本文由纯净天空筛选整理自pp_pankaj大神的英文原创作品 YearMonth now(clock) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。