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 -
This tutorial first explains the important points to keep in mind before using
OUTPUT of the above code
Explanation of the code
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.DayOfWeekis an Enum which defines7constants representing the seven days of the week -MONDAY(int value=1),TUESDAY(2),WEDNESDAY(3)... tillSUNDAY(7).- It is recommended to use the
Enumconstants in code rather than their equivalentintvalues to enhance code readability and clarity. - Lastly, in case the use of
intvalues is required, then the value obtained for each of the constants obtained usinggetValue()method should be used, and use ofordinal()method should be avoided.
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);
}
}
2016-01-01 was a FRIDAY First working day of 2016 was 2016-01-04
- The
DayOfWeekExampleclass first creates an instance ofjava.time.LocalDateclass, named localDate, using itsof()method. The variable holds the date01-Jan-2016. - An instance of
java.time.DayOfWeek, nameddayOfWeek, is obtained fromlocalDateusing theLocalDate.getDayOfWeek()method. The day of the week on01-Jan-2016was aFRIDAYand the same is printed as output. - The next section of the code shows the conditional usage of
DayOfWeekinstance by using it to determine the first working day of the year. - The variable
dayOfWeekis passed to aswitch()statement and it has 3 cases -- If the
dayOfWeekisFRIDAYthen2days are added to thelocalDateto get the date for the nearestMONDAY.2days are added usingLocalDate.plusDays()method, and assigned to theLocalDateinstance namedfirstWorkingDay. - If
dayOfWeekisSATURDAYthen a single day is added usingplusDays(1)and assigned tofirstWorkingDay. - 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 usingplusDays(1).
- If the
- The value determined for
firstWorkingDayis then correctly printed as2016-01-04. - Note - The code for using day of week remains the same if you want to use
LocalDateTimeinstead ofLocalDateused above. Just changeLocalDatetoLocalDateTimeand the rest of the code, including method name, will remain the same.
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.Tutorials on Java 8’s new Date and Time API
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters