Java YearMonth atEndOfMonth()方法
Java中YearMonth类的atEndOfMonth()方法用于返回一个基于该YearMonth对象的每月最后一天的LocalDate。
语法:
public LocalDate atEndOfMonth()
参数 :该方法不接受任何参数。
返回值 :它返回一个本地日期,该日期是由这个YearMonth对象指定的当前月份的最后一天。该方法不返回一个空值。
以下程序说明了Java中YearMonth的atEndOfMonth()方法:
程序1 :
// Programt to illustrate the atEndOfMonth() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Creates a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2017, 8);
// Creates a local date with this
// YearMonth object passed to it
// Last day of this month is 31
LocalDate date = thisYearMonth.atEndOfMonth();
System.out.println(date);
}
}
输出。
2017-08-31
程序2 :这种方法也照顾到了闰年。
// Programt to illustrate the atEndOfMonth() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Creates a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2016, 2);
// Creates a local date with this
// YearMonth object passed to it
// Last day of February is
// 29 as 2016 is a leap year
LocalDate date = thisYearMonth.atEndOfMonth();
System.out.println(date);
}
}
输出。
2016-02-29
参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#atEndOfMonth-