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