Java Check if OffsetDateTime is Week Day or Weekend Day

Tags: Java OffsetDateTime DayOfWeek DateUtil

In this Java tutorial, we learn how to check if a given OffsetDateTime object is a week day or weekend day in Java programming language.

How to check if OffsetDateTime object is week day or weekend day in Java

At this first step, we implement a new Java class named DateUtil, and implement these two methods.

  • isWeekDay(OffsetDateTime offsetDateTime) static method to check if a given OffsetDateTime object if a week day or not.
  • isWeekendDay(OffsetDateTime offsetDateTime) static method to check if a given OffsetDateTime object if a weekend day or not.

DateUtil.java

import java.time.DayOfWeek;
import java.time.OffsetDateTime;

public class DateUtil {

    /**
     * This method to check if an OffsetDateTime value is a week day or not.
     * @param offsetDateTime
     * @return
     */
    public static boolean isWeekDay(OffsetDateTime offsetDateTime) {
        DayOfWeek dayOfWeek = offsetDateTime.getDayOfWeek();
        return (dayOfWeek != DayOfWeek.SATURDAY)
                && (dayOfWeek != DayOfWeek.SUNDAY);
    }

    /**
     * This method to check if an OffsetDateTime value is a weekend day or not.
     * @param offsetDateTime
     * @return
     */
    public static boolean isWeekendDay(OffsetDateTime offsetDateTime) {
        DayOfWeek dayOfWeek = offsetDateTime.getDayOfWeek();
        return (dayOfWeek == DayOfWeek.SATURDAY)
                || (dayOfWeek == DayOfWeek.SUNDAY);
    }
}

In the following example Java code, we learn how to use the above DateUtil class to check OffsetDateTime values whether it is week day or weekend day in Java program.

CheckOffsetDateTimeWeekDayAndWeekendExample.java

import java.time.OffsetDateTime;

public class CheckOffsetDateTimeWeekDayAndWeekendExample {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-08-21T08:00:00.00+07:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-08-22T08:00:00.00+07:00");

        // Check if an OffsetDateTime is Week Day or not
        boolean isWeekDay1 = DateUtil.isWeekDay(offsetDateTime1);
        boolean isWeekDay2 = DateUtil.isWeekDay(offsetDateTime2);

        // Check if an OffsetDateTime is Weekend Day or not
        boolean isWeekend1 = DateUtil.isWeekendDay(offsetDateTime1);
        boolean isWeekend2 = DateUtil.isWeekendDay(offsetDateTime2);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);

        System.out.println("\noffsetDateTime1 is week day: " + isWeekDay1);
        System.out.println("offsetDateTime2 is week day: " + isWeekDay2);

        System.out.println("\noffsetDateTime1 is weekend day: " + isWeekend1);
        System.out.println("offsetDateTime2 is weekend day: " + isWeekend2);
    }
}
The output as below.
offsetDateTime1: 2022-08-21T08:00+07:00
offsetDateTime2: 2022-08-22T08:00+07:00

offsetDateTime1 is week day: false
offsetDateTime2 is week day: true

offsetDateTime1 is weekend day: true
offsetDateTime2 is weekend day: false

Happy Coding 😊

Java Check if Today is Week Day or Weekend Day

Java Check if Calendar is Week Day or Weekend Day

Java Check if Date is Week Day or Weekend Day

Java Check if Instant is Week Day or Weekend Day

Java Check if LocalDate is Week Day or Weekend Day

Java Check if LocalDateTime is Week Day or Weekend Day

Java Check if ZonedDateTime is Week Day or Weekend Day