Java Check if Calendar is Week Day or Weekend Day

Tags: Java Calendar DateUtil

In this Java tutorial, we learn how to write a Java utility class to check if a Calendar date is a week day or a weekend day in Java programming language.

How to check if Calendar is week day or weekend day in Java

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

  • isWeekDay(Calendar calendar) to check a given Calendar object whether it is a week day or not.
  • isWeekend(Calendar calendar) to check a given Calendar object whether it is a weekend day or not.

DateUtil.java

import java.util.Calendar;

public class DateUtil {

    /**
     * This method to check a given Calendar date is a week day or not.
     * @param calendar
     * @return
     */
    public static boolean isWeekDay(Calendar calendar) {
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return (dayOfWeek != Calendar.SATURDAY)
                && (dayOfWeek != Calendar.SUNDAY);
    }

    /**
     * This method to check a given Calendar date is a weekend day or not.
     * @param calendar
     * @return
     */
    public static boolean isWeekend(Calendar calendar) {
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return (dayOfWeek == Calendar.SATURDAY)
                || (dayOfWeek == Calendar.SUNDAY);
    }

}

In the following example Java code, we learn how to use the DateUtil class above in Java program to check if a given Calendar object is a week day or weekend day.

CheckCalendarWeekDayAndWeekendExample.java

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CheckCalendarWeekDayAndWeekendExample {
    public static void main(String... args) {
        Calendar calendar1 = new GregorianCalendar(2022, Calendar.AUGUST, 14);
        Calendar calendar2 = new GregorianCalendar(2022, Calendar.AUGUST, 15);

				// Check if Calendar is Week Day or not
        boolean isWeekDay1 = DateUtil.isWeekDay(calendar1);
        boolean isWeekDay2 = DateUtil.isWeekDay(calendar2);

				// Check if Calendar is Weekend Day or not
        boolean isWeekend1 = DateUtil.isWeekend(calendar1);
        boolean isWeekend2 = DateUtil.isWeekend(calendar2);

        System.out.println("calendar1: " + calendar1.getTime());
        System.out.println("calendar2: " + calendar2.getTime());

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

        System.out.println("\ncalendar1 is weekend: " + isWeekend1);
        System.out.println("calendar2 is weekend: " + isWeekend2);
    }
}
The output as below.
calendar1: Sun Aug 14 00:00:00 ICT 2022
calendar2: Mon Aug 15 00:00:00 ICT 2022

calendar1 is week day: false
calendar2 is week day: true

calendar1 is weekend: true
calendar2 is weekend: false

Happy Coding 😊

Java Check if Date in between Start and End Dates

Java Check if Date is Week Day or Weekend Day

Java Check if Today 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 OffsetDateTime is Week Day or Weekend Day

Java Check if ZonedDateTime is Week Day or Weekend Day

Java Get First Day of Month from Specified Date

Java Get Yesterday Date

Java Get Tomorrow Date

Java Get Last Date of Current Month

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Convert String to Date

Java Convert Date to Start of Day Time

Java Convert Date to End of Day Time

Java Get First Date of Current Year

Java Get First Date of Current Month

Java Get Same Date in Last Month