📜  Java中的 YearMonth now() 方法及示例

📅  最后修改于: 2022-05-13 01:55:06.266000             🧑  作者: Mango

Java中的 YearMonth now() 方法及示例

Java中YearMonth类的now()方法用于从默认时区的系统时钟中获取当前的年月。

句法:

public static YearMonth now()

参数:此方法不接受任何参数。

返回值:该方法使用系统时钟和默认时区返回当前的年月

下面的程序说明了Java中 YearMonth 的 now() 方法:

方案一:

// 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

方案二:

// 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: Java()