Java Check if Instant is Week Day or Weekend Day

Tags: Java Instant DayOfWeek DateUtil

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

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

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

  • isWeekDay(Instant instant) to check if a given Instant value is a week day or not.
  • isWeekend(Instant instant) to check if a given Instant value is a weekend day or not.

DateUtil.java

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class DateUtil {

    /**
     * This method to check if an Instant object is a week day or not.
     * @param instant
     * @return
     */
    public static boolean isWeekDay(Instant instant) {
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
        return (dayOfWeek != DayOfWeek.SATURDAY)
                && (dayOfWeek != DayOfWeek.SUNDAY);
    }

    /**
     * This method to check if an Instant object is a weekend day or not.
     * @param instant
     * @return
     */
    public static boolean isWeekend(Instant instant) {
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
        return (dayOfWeek == DayOfWeek.SATURDAY)
                || (dayOfWeek == DayOfWeek.SUNDAY);
    }
}

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

CheckInstantWeekDayAndWeekendExample.java

import java.time.Instant;

public class CheckInstantWeekDayAndWeekendExample {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-08-19T07:30:40.00Z");
        Instant instant2 = Instant.parse("2022-08-20T07:30:40.00Z");

        // Check if an Instant is Week Day or not
        boolean isWeekDay1 = DateUtil.isWeekDay(instant1);
        boolean isWeekDay2 = DateUtil.isWeekDay(instant2);

        // Check if an Instant is Weekend Day or not
        boolean isWeekend1 = DateUtil.isWeekend(instant1);
        boolean isWeekend2 = DateUtil.isWeekend(instant2);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);

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

        System.out.println("\ninstant1 is weekend: " + isWeekend1);
        System.out.println("instant2 is weekend: " + isWeekend2);
    }
}
The output as below.
instant1: 2022-08-19T07:30:40Z
instant2: 2022-08-20T07:30:40Z

instant1 is week day: true
instant2 is week day: false

instant1 is weekend: false
instant2 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 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