Java中的 TemporalAdjusters lastDayOfMonth() 方法及示例
TemporalAdjusters 类的lastDayOfMonth()方法用于返回“本月的最后一天”TemporalAdjuster 对象,该对象返回一个设置为该月最后一天的新 Date 对象。
句法:
public static TemporalAdjuster lastDayOfMonth()
参数:此方法不接受任何内容。
返回值:该方法返回月度调整器的最后一天,不为空。
下面的程序说明了 TemporalAdjusters.lastDayOfMonth() 方法:
方案一:
// Java program to demonstrate
// TemporalAdjusters.lastDayOfMonth()
import java.time.LocalDate;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// get TemporalAdjuster with last day
// of the month adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters.lastDayOfMonth();
// using adjuster for local date-time
LocalDate localDate
= LocalDate.of(2020, 05, 11);
LocalDate lastDayOfMonth
= localDate.with(temporalAdjuster);
// print
System.out.println("last day of the "
+ "month for localdate "
+ localDate + ": "
+ lastDayOfMonth);
}
}
输出:
last day of the month for localdate 2020-05-11: 2020-05-31
方案二:
// Java program to demonstrate
// TemporalAdjusters.lastDayOfMonth() method
import java.time.LocalDate;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// get TemporalAdjuster with last day
// of the month adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters
.lastDayOfMonth();
// using adjuster for local date-time
LocalDate localDate
= LocalDate.of(2010, 12, 29);
LocalDate lastDayOfMonth
= localDate.with(temporalAdjuster);
// print
System.out.println("last day of the "
+ "month for localdate "
+ localDate + ": "
+ lastDayOfMonth);
}
}
输出:
last day of the month for localdate 2010-12-29: 2010-12-31
参考资料:https: Java