📜  Java中的 MonthDay now() 方法和示例

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

Java中的 MonthDay now() 方法和示例

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

句法:

public static MonthDay now()

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

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

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

方案一:

// Java program to demonstrate
// MonthDay.now() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // apply now() method
        // of MonthDay class
        MonthDay result = MonthDay.now();
  
        // print both month and day
        System.out.println("MonthDay: "
                           + result);
    }
}
输出:
MonthDay: --05-09

方案二:

// Java program to demonstrate
// MonthDay.now() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // apply now() method
        // of MonthDay class
        MonthDay result = MonthDay.now();
  
        // print only month
        System.out.println("Month: "
                           + result.getMonth());
    }
}
输出:
Month: MAY

参考资料: https: Java()