Java MonthDay atYear()方法及示例

来源:这里教程网 时间:2026-02-17 20:58:58 作者:

Java MonthDay atYear()方法及示例

Java中MonthDay类的 atYear() 方法将这个月日与一个年份结合起来,创建一个LocalDate。

语法

public LocalDate atYear(int year)

参数。该方法接受一个参数year,指定使用的年份,范围是[MIN_YEAR, MAX_YEAR] 。

返回: 该函数返回由该月日和指定年份形成的本地日期,而不是空值。

下面的程序说明了 MonthDay.atYear() 方法。

程序1 :

// Program to illustrate the atYear() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Parses the date        MonthDay tm = MonthDay.parse("--12-06");          // Uses the function        LocalDate dt = tm.atYear(2018);          // Prints the date        System.out.println(dt);    }}

输出。

2018-12-06

程序2

// Program to illustrate the atYear() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Parses the date        MonthDay tm = MonthDay.parse("--01-01");          // Uses the function        LocalDate dt = tm.atYear(2010);          // Prints the date        System.out.println(dt);    }}

输出。

2010-01-01

参考资料: https://docs.oracle.com/javase/8/docs/api/java/time/MonthDay.html#atYear-int-

相关推荐