How do you get the first day of the month using LocalDate in Java?

In Java, you can use the withDayOfMonth() method of the LocalDate class to get the first day of the current month. The specific implementation is as follows:

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate firstDayOfMonth = now.withDayOfMonth(1);
        System.out.println("当月第一天:" + firstDayOfMonth);
    }
}

Running the above code will output the first day of the current month.

bannerAds