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