Java Check if Date is Week Day or Weekend Day

Tags: Java Date DateUtil

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

How to check if Date is a week day or weekend day in Java

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

  • isWeekDay(Date date) method to check if a given Date object is a week day or not.
  • isWeekend(Date date) method to check if a given Date object is a weekend day or not.

DateUtil.java

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

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

    /**
     * This method to check if a given Date is weekend day or not.
     * @param date
     * @return
     */
    public static boolean isWeekend(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        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 to check a given Date if it is a week day or weekend day.

CheckDateWeekDayAndWeekendExample.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckDateWeekDayAndWeekendExample {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/08/14 08:30:00");
        Date date2 = simpleDateFormat.parse("2022/08/15 08:30:00");

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

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

        System.out.println("date1: " + date1);
        System.out.println("date2: " + date2);

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

        System.out.println("\ndate1 is weekend: " + isWeekend1);
        System.out.println("date2 is weekend: " + isWeekend2);
    }
}
The output as below.
date1: Sun Aug 14 08:30:00 ICT 2022
date2: Mon Aug 15 08:30:00 ICT 2022

date1 is week day: false
date2 is week day: true

date1 is weekend: true
date2 is weekend: false

Happy Coding 😊

Java Check if Date in between Start and End Dates

Java Check if Calendar 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