Java 8 – How to get day of week for a given date using java.time.DayOfWeek with examples

There are many instances when the business requirements specify implementation logic based on day of the week(Monday, Tuesday…) for a given date. Java 8 has defined a separate Enum for handling days of the week named – DayOfWeek (java.time.DayOfWeek).

This tutorial first explains the important points to keep in mind before using DayOfWeek Enum. It then explains how to use DayOfWeek for commonly used scenarios of getting the day of the week for a given date, and then taking a decision based on the day of the week determined with an example program.

Important points regarding DayOfWeek Enum

  • java.time.DayOfWeek is an Enum which defines 7 constants representing the seven days of the week – MONDAY(int value=1), TUESDAY(2), WEDNESDAY(3)… till SUNDAY(7).
  • It is recommended to use the Enum constants in code rather than their equivalent int values to enhance code readability and clarity.
  • Lastly, in case the use of int values is required, then the value obtained for each of the constants obtained using getValue() method should be used, and use of ordinal() method should be avoided.

Determining the day of week, and executing conditional logic, for a given date

Java 8 code example showing DayOfWeek usage
package com.javabrahman.java8.time;
import java.time.LocalDate;
public class DayOfWeekExample {
  public static void main(String args[]) {
    LocalDate localDate = LocalDate.of(2016, 01, 01);
    
    //Getting the day of week for a given date
    java.time.DayOfWeek dayOfWeek = localDate.getDayOfWeek();
    System.out.println(localDate + " was a " + dayOfWeek);
    LocalDate firstWorkingDay;
    
    //Using DayOfWeek to execute dependent business logic
    switch (dayOfWeek) {
      case FRIDAY:firstWorkingDay = localDate.plusDays(3);
        break;
      case SATURDAY:firstWorkingDay = localDate.plusDays(2);
        break;
      default:firstWorkingDay=localDate.plusDays(1);
        break;
    }
    System.out.println("First working day of 2016 was "+firstWorkingDay);
  }
}
 OUTPUT of the above code
2016-01-01 was a FRIDAY
First working day of 2016 was 2016-01-04
Explanation of the code

  • The DayOfWeekExample class first creates an instance of java.time.LocalDate class, named localDate, using its of() method. The variable holds the date 01-Jan-2016.
  • An instance of java.time.DayOfWeek, named dayOfWeek, is obtained from localDate using the LocalDate.getDayOfWeek() method. The day of the week on 01-Jan-2016 was a FRIDAY and the same is printed as output.
  • The next section of the code shows the conditional usage of DayOfWeek instance by using it to determine the first working day of the year.
  • The variable dayOfWeek is passed to a switch() statement and it has 3 cases –
    1. If the dayOfWeek is FRIDAY then 2 days are added to the localDate to get the date for the nearest MONDAY. 2 days are added using LocalDate.plusDays() method, and assigned to the LocalDate instance named firstWorkingDay.
    2. If dayOfWeek is SATURDAY then a single day is added using plusDays(1) and assigned to firstWorkingDay.
    3. In all other cases, i.e. default, it is a weekday falling between MONDAY TO THURSDAY, in which case the next working is the first working day of the year, which is obtained using plusDays(1).
  • The value determined for firstWorkingDay is then correctly printed as 2016-01-04.
  • Note – The code for using day of week remains the same if you want to use LocalDateTime instead of LocalDate used above. Just change LocalDate to LocalDateTime and the rest of the code, including method name, will remain the same.

Summary
In this tutorial we understood how to get day of week for a given date using java.time.DayOfWeek enum along with its usage for applying business logic based on the day. Given below is a list of more Java 8 Date-Time tutorials for your reference.

 

Digiprove sealCopyright © 2014-2017 JavaBrahman.com, all rights reserved.