Java Program to get day of week for the last day of each month in 2019

Java 8Object Oriented ProgrammingProgramming

For last day of each month, at first create a List collection for day of week −

List<DayOfWeek>dayOfWeek = new ArrayList<>();

Now, get day of week for the last day of each month in 2019, set the year using withYear() and use lastDayOfMonth() and getDayOfWeek methods −

for (Month m: Month.values()) {
   DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek();
}


Example

 Live Demo

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String[] argv) {
      List<DayOfWeek>dayOfWeek = new ArrayList<>();
      for (Month m: Month.values()) {
         DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek();
         dayOfWeek.add(days);
      }
      System.out.println("Last day of each month in 2019 = "+dayOfWeek);
   }
}

Output

Last day of each month in 2019 = [THURSDAY, THURSDAY, SUNDAY, TUESDAY, FRIDAY, SUNDAY, WEDNESDAY, SATURDAY, MONDAY, THURSDAY, SATURDAY, TUESDAY]
raja
Published on 25-Apr-2019 13:19:56
Advertisements