Java中YearMonth类的now()方法用于在默认时区中从系统时钟获取当前的year-month。
用法:
public static YearMonth now()
参数:此方法不接受任何参数。
返回值:此方法使用系统时钟和默认时区返回当前的year-month。
以下示例程序旨在说明Java中YearMonth的now()方法:
程序1:
// Java program to demonstrate
// YearMonth.now() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// apply now() method
// of YearMonth class
YearMonth result = YearMonth.now();
// print both year and month
System.out.println("YearMonth:"
+ result);
}
}
输出:
YearMonth:2020-05
程序2:
// Java program to demonstrate
// YearMonth.now() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// apply now() method
// of YearMonth class
YearMonth result = YearMonth.now();
// 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 YearMonth with()用法及代码示例
- Java YearMonth until()用法及代码示例
- Java YearMonth get()用法及代码示例
- Java YearMonth withMonth()用法及代码示例
- Java YearMonth range()用法及代码示例
- Java YearMonth minusMonths()用法及代码示例
- Java YearMonth withYear()用法及代码示例
- Java YearMonth now(clock)用法及代码示例
- 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() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。