Java DayOfWeek

Introduction

DayOfWeek in Java, part of the java.time package, is an enum that represents the days of the week. It provides various methods to manipulate and retrieve information about weekdays.

Table of Contents

  1. What is DayOfWeek?
  2. Using DayOfWeek
  3. Common Methods
  4. Examples of DayOfWeek
  5. Conclusion

1. What is DayOfWeek?

DayOfWeek is an enum representing the seven days of the week, from Monday to Sunday. It is used in date-time calculations and manipulations.

2. Using DayOfWeek

You can use DayOfWeek to perform various operations, such as getting the current day, adjusting dates, and calculating days until or since a particular day.

3. Common Methods

  • DayOfWeek.of(int dayOfWeek): Obtains an instance of DayOfWeek from an integer value.
  • getValue(): Returns the day-of-week, from 1 (Monday) to 7 (Sunday).
  • plus(long days): Returns the day-of-week that is the specified number of days after this one.
  • minus(long days): Returns the day-of-week that is the specified number of days before this one.
  • from(TemporalAccessor temporal): Obtains an instance of DayOfWeek from a temporal object.

4. Examples of DayOfWeek

Example 1: Getting the Current Day of the Week

This example demonstrates how to get the current day of the week.

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

public class CurrentDayOfWeekExample {
    public static void main(String[] args) {
        DayOfWeek today = LocalDate.now().getDayOfWeek();
        System.out.println("Today is: " + today);
    }
}

Output:

Today is: SUNDAY

Example 2: Using DayOfWeek.of()

This example shows how to create a DayOfWeek instance using an integer.

import java.time.DayOfWeek;

public class DayOfWeekOfExample {
    public static void main(String[] args) {
        DayOfWeek day = DayOfWeek.of(5); // Friday
        System.out.println("Day of the Week: " + day);
    }
}

Output:

Day of the Week: FRIDAY

Example 3: Adding Days to a DayOfWeek

This example demonstrates how to add days to a DayOfWeek instance.

import java.time.DayOfWeek;

public class AddDaysExample {
    public static void main(String[] args) {
        DayOfWeek day = DayOfWeek.WEDNESDAY;
        DayOfWeek result = day.plus(3); // Adding 3 days
        System.out.println("Day after 3 days: " + result);
    }
}

Output:

Day after 3 days: SATURDAY

Example 4: Subtracting Days from a DayOfWeek

This example shows how to subtract days from a DayOfWeek instance.

import java.time.DayOfWeek;

public class SubtractDaysExample {
    public static void main(String[] args) {
        DayOfWeek day = DayOfWeek.MONDAY;
        DayOfWeek result = day.minus(2); // Subtracting 2 days
        System.out.println("Day before 2 days: " + result);
    }
}

Output:

Day before 2 days: SATURDAY

Conclusion

DayOfWeek is a helpful enum in Java for managing and manipulating the days of the week. It provides methods for arithmetic operations and conversions, making it a versatile tool in date-time calculations. Using DayOfWeek can lead to cleaner and more intuitive code when working with weekdays.

Comments