Java Check if LocalDate is Week Day or Weekend Day

Tags: Java LocalDate DayOfWeek DateUtil

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

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

In this first step, we create a new Java utility class named DateUtil, and implement two static method below.

  • isWeekDay(LocalDate localDate) static method to check if a given LocalDate value is a week day or not.
  • isWeekend(LocalDate localDate) static method to check if a given LocalDate value is a weekend day or not.

DateUtil.java

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

public class DateUtil {

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

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

In the following example Java code, we learn how to use the above methods to check LocalDate objects whether it is a week day or weekend day in Java program.

CheckLocalDateWeekDayAndWeekendExample.java

import java.time.LocalDate;

public class CheckLocalDateWeekDayAndWeekendExample {
    public static void main(String... args) {
        LocalDate localDate1 = LocalDate.of(2022, 8, 19);
        LocalDate localDate2 = LocalDate.of(2022, 8, 20);

        // Check if an LocalDate is Week Day or not
        boolean isWeekDay1 = DateUtil.isWeekDay(localDate1);
        boolean isWeekDay2 = DateUtil.isWeekDay(localDate2);

        // Check if an LocalDate is Weekend Day or not
        boolean isWeekend1 = DateUtil.isWeekend(localDate1);
        boolean isWeekend2 = DateUtil.isWeekend(localDate2);

        System.out.println("localDate1: " + localDate1);
        System.out.println("localDate2: " + localDate2);

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

        System.out.println("\nlocalDate1 is weekend: " + isWeekend1);
        System.out.println("localDate2 is weekend: " + isWeekend2);
    }
}
The output as below.
localDate1: 2022-08-19
localDate2: 2022-08-20

localDate1 is week day: true
localDate2 is week day: false

localDate1 is weekend: false
localDate2 is weekend: true

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 LocalDateTime is Week Day or Weekend Day

Java Check if OffsetDateTime is Week Day or Weekend Day

Java Check if ZonedDateTime is Week Day or Weekend Day