Java DayOfWeek Enum Methods with Examples

This article is the series of Java Date Time API TutorialIn this article, we will discuss important methods or APIs of the Java DayOfWeek enum from the java.time package.

DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). It is recommended that applications use the enum rather than the int value to ensure code clarity.
Important : Do not use ordinal() to obtain the numeric representation of DayOfWeek. Use getValue() instead.

Java DayOfWeek Enum Class Diagram

The below class diagram shows a list of methods that the DayOfWeek enum provides.

Java DayOfWeek Enum Methods/APIs with Examples

DayOfWeek Enum Methods/APIs

Let's list all important DayOfWeek enum methods and the next section will show it's usage with an example.
  • static DayOfWeek from(TemporalAccessor temporal) - Obtains an instance of DayOfWeek from a temporal object.
  • int get(TemporalField field) - Gets the value of the specified field from this day-of-week as an int.
  • String getDisplayName(TextStyle style, Locale locale) - Gets the textual representation, such as 'Mon' or 'Friday'.
  • long getLong(TemporalField field) - Gets the value of the specified field from this day-of-week as a long.
  • int getValue() - Gets the day-of-week int value.
  • boolean isSupported(TemporalField field) - Checks if the specified field is supported.
  • DayOfWeek minus(long days) - Returns the day-of-week that is the specified number of days before this one.
  • static DayOfWeek of(int dayOfWeek) - Obtains an instance of DayOfWeek from an int value.
  • DayOfWeek plus(long days) - Returns the day-of-week that is the specified number of days after this one.
  • R query(TemporalQuery query) - Queries this day-of-week using the specified query.
  • ValueRange range(TemporalField field) - Gets the range of valid values for the specified field.
  • static DayOfWeek valueOf(String name) - Returns the enum constant of this type with the specified name.
  • static DayOfWeek[] values() - Returns an array containing the constants of this enum type, in the order, they are declared.

DayOfWeek Enum Methods/APIs Examples

Let's demonstrates the usage of a few important and commonly used DayOfWeek Enum methods with examples. In below DayOfWeekMethodExamples class, each method name describes the DayOfWeek enum method and its usage.
package net.javaguides.date;

import java.time.DayOfWeek;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
import java.util.Locale;

/**
 * Class demonstrates the usage of DayOfWeek methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class DayOfWeekMethodExamples {

    public static void main(String[] args) {
        DayOfWeekMethodExamples examples = new DayOfWeekMethodExamples();
        examples.valuesMethodExample();
        examples.valueOfMethodExample();
        examples.rangeMethodExample();
        examples.queryMethodExample();
        examples.plusMethodExample();
        examples.ofMethodExample();
        examples.minusMethodExample();
        examples.getValueMethodExample();
        examples.getLongMethodExample();
        examples.getDisplayNameMethodExample();
        examples.getMethodExample();
        examples.fromMethodExample();
    }

    public void valuesMethodExample() {
        for (DayOfWeek day: DayOfWeek.values()) {
            System.out.print(" " + day);
        }
    }

    public void valueOfMethodExample() {
        DayOfWeek day = DayOfWeek.valueOf("WEDNESDAY");
        System.out.println(day);
    }

    public void rangeMethodExample() {
        DayOfWeek day = DayOfWeek.of(2);
        System.out.println("Range : " + day.range(ChronoField.DAY_OF_WEEK));
    }

    public void queryMethodExample() {
        DayOfWeek day = DayOfWeek.of(2);
        System.out.printf("DayOfWeek precision is %s%n", day.query(TemporalQueries.precision()));
    }

    public void plusMethodExample() {
        DayOfWeek day = DayOfWeek.of(5);
        System.out.println(day);
        System.out.println(day.plus(1));
    }

    public void ofMethodExample() {
        DayOfWeek day = DayOfWeek.of(3);
        System.out.println(day);
    }

    public void minusMethodExample() {
        DayOfWeek day = DayOfWeek.of(5);
        System.out.println(day);
        System.out.println(day.minus(1));
    }

    public void getValueMethodExample() {
        DayOfWeek day = DayOfWeek.of(3);
        System.out.println(day.getValue());
    }

    public void getLongMethodExample() {
        DayOfWeek day = DayOfWeek.of(3);
        System.out.println(day.getLong(ChronoField.DAY_OF_WEEK));
    }

    public void getDisplayNameMethodExample() {
        DayOfWeek day = DayOfWeek.of(3);
        System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
    }

    public void getMethodExample() {
        DayOfWeek day = DayOfWeek.FRIDAY;
        System.out.println(day.get(ChronoField.DAY_OF_WEEK));
    }

    public void fromMethodExample() {
        DayOfWeek day = DayOfWeek.from(ZonedDateTime.now());
        System.out.println(day);
    }
}
Output:
 MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAYWEDNESDAY
Range : 1 - 7
DayOfWeek precision is Days
FRIDAY
SATURDAY
WEDNESDAY
FRIDAY
THURSDAY
3
3
Wed
5
SATURDAY

Related Java 8 Date & Time API Guide

References

Comments