Java - Day of Week

Introduction

DayOfWeek enum has seven constants to represent seven days of the week.

The constants are MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY.

Its getValue() method returns an int value: 1 for Monday, 2 for Tuesday, and so on, which follows ISO-8601 standards.

Below are some examples of using the DayOfWeek enum and its methods.

Demo

import java.time.DayOfWeek;
import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2012, 5, 10);

    // Extract the day-of-week from a LocalDate
    DayOfWeek dw1 = DayOfWeek.from(ld); // THURSDAY
    System.out.println(dw1);//from  w w w.  ja va2 s  .co m
    // Get the int value of the day-of-week
    int dw11 = dw1.getValue(); // 4
    System.out.println(dw11);
    // Use the method of the LocalDate class to get day-of-week
    DayOfWeek dw5 = ld.getDayOfWeek(); // THURSDAY
    System.out.println(dw5);
    // Obtain a DayOfWeek instance using an int value
    DayOfWeek dw2 = DayOfWeek.of(7); // SUNDAY
    System.out.println(dw2);
    // Add one day to the day-of-week to get the next day
    DayOfWeek dw3 = dw2.plus(1); // MONDAY
    System.out.println(dw3);
    // Get the day-of-week two days ago
    DayOfWeek dw4 = dw2.minus(2); // FRIDAY
    System.out.println(dw4);

  }
}

Result