📌  相关文章
📜  Java中的 TemporalAdjusters firstDayOfMonth() 方法及示例

📅  最后修改于: 2022-05-13 01:55:22.326000             🧑  作者: Mango

Java中的 TemporalAdjusters firstDayOfMonth() 方法及示例

TemporalAdjusters 类firstDayOfMonth()方法用于返回“本月的第一天”TemporalAdjuster 对象,该对象返回一个设置为当月第一天的新 Date 对象。

句法:

public static TemporalAdjuster firstDayOfMonth()

参数:此方法不接受任何内容。

返回值:此方法返回月的第一天调整器,不为空。

下面的程序说明了 TemporalAdjusters.firstDayOfMonth() 方法:
方案一:

// Java program to demonstrate
// TemporalAdjusters.firstDayOfMonth()
  
import java.time.LocalDate;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with first day
        // of month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .firstDayOfMonth();
  
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2020, 05, 11);
        LocalDate firstDayOFMonth
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("First day for "
                           + "month for localdate "
                           + localDate + ": "
                           + firstDayOFMonth);
    }
}
输出:
First day for month for localdate 2020-05-11: 2020-05-01

方案二:

// Java program to demonstrate
// TemporalAdjusters.firstDayOfMonth() 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 first day
        // of month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .firstDayOfMonth();
  
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2010, 12, 29);
        LocalDate firstDayOFMonth
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("First day for "
                           + "month for localdate "
                           + localDate + ": "
                           + firstDayOFMonth);
    }
}
输出:
First day for month for localdate 2010-12-29: 2010-12-01

参考资料:https: Java