Java Program To Print First And Last Day Of Month
Chapter:
Miscellaneous
Last Updated:
11-08-2023 16:55:08 UTC
Program:
/* ............... START ............... */
import java.time.LocalDate;
import java.time.YearMonth;
public class FirstAndLastDayOfMonth {
public static void main(String[] args) {
int year = 2023; // Replace with the desired year
int month = 8; // Replace with the desired month
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate firstDayOfMonth = yearMonth.atDay(1);
LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
System.out.println("First day of " + yearMonth + ": " + firstDayOfMonth);
System.out.println("Last day of " + yearMonth + ": " + lastDayOfMonth);
}
}
/* ............... END ............... */
Output
First day of 2023-08: 2023-08-01
Last day of 2023-08: 2023-08-31
Notes:
-
Replace the year and month variables with the desired year and month for which you want to find the first and last day. The program uses the YearMonth class from the java.time package introduced in Java 8 to calculate the first and last day of the specified month and year.