Cheap VPS host selection
Provide server host evaluation information

Get the first day of the month for LocalDate in Java

In Java, you can use the LocalDate class and the TemporalAdjusters class to get the first day of the month. The following is sample code:

 import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public  class  Example { public  static  void  main (String[] args) { //Get the current date
         LocalDate  today  = LocalDate.now(); //Use TemporalAdjusters to get the first day of the month
         LocalDate  firstDayOfMonth  = today.with(TemporalAdjusters.firstDayOfMonth()); //Output results System.out.println( First day of the month: + firstDayOfMonth); } }

In the above code, we first use the LocalDate. now() method to obtain the current date. Then, use the TemporalAdjusters. firstDayOfMonth() method to get the first day of the month, and assign it to the firstDayOfMonth variable. Finally, we output the results, that is, the first day of the month.

Note that the time part of the first day of the month obtained is 0:0:0 by default. If you need to specify a specific time, you can create a new LocalDate object through the LocalDate. of (year, month, dayOfMonth) method, where year, month, and dayOfMonth respectively represent the year, month, and date.

Do not reprint without permission: Cheap VPS evaluation » Get the first day of the month for LocalDate in Java