Java Check if ZonedDateTime is Week Day or Weekend Day
Tags: Java ZonedDateTime DayOfWeek DateUtil
In this Java tutorial, we learn how to check if a given ZonedDateTime object is a week day or weekend day in Java programming language.
How to check if ZonedDateTime is week day or weekend day in Java
In this first step, we create a new Java class named DateUtil, and add these two static methods.
- isWeekDay(ZonedDateTime zonedDateTime) to check if a given ZonedDateTime value is a week day or not.
- isWeekendDay(ZonedDateTime zonedDateTime) to check if a given ZonedDateTime value is a weekend day or not.
DateUtil.java
import java.time.DayOfWeek;
import java.time.ZonedDateTime;
public class DateUtil {
/**
* This method to check if a ZonedDateTime value is a week day or not.
* @param zonedDateTime
* @return
*/
public static boolean isWeekDay(ZonedDateTime zonedDateTime) {
DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
return (dayOfWeek != DayOfWeek.SATURDAY)
&& (dayOfWeek != DayOfWeek.SUNDAY);
}
/**
* This method to check if a ZonedDateTime value is a weekend day or not.
* @param zonedDateTime
* @return
*/
public static boolean isWeekendDay(ZonedDateTime zonedDateTime) {
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 ZonedDateTime values whether it is week day or weekend day in Java program.
CheckZonedDateTimeWeekDayAndWeekendExample.java
import java.time.ZonedDateTime;
public class CheckZonedDateTimeWeekDayAndWeekendExample {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-08-21T08:15:30+10:00[Australia/Sydney]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-08-22T08:15:30+10:00[Australia/Sydney]");
// Check if an ZonedDateTime is Week Day or not
boolean isWeekDay1 = DateUtil.isWeekDay(zonedDateTime1);
boolean isWeekDay2 = DateUtil.isWeekDay(zonedDateTime2);
// Check if an ZonedDateTime is Weekend Day or not
boolean isWeekend1 = DateUtil.isWeekendDay(zonedDateTime1);
boolean isWeekend2 = DateUtil.isWeekendDay(zonedDateTime2);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("\nzonedDateTime1 is week day: " + isWeekDay1);
System.out.println("zonedDateTime2 is week day: " + isWeekDay2);
System.out.println("\nzonedDateTime1 is weekend day: " + isWeekend1);
System.out.println("zonedDateTime2 is weekend day: " + isWeekend2);
}
}
zonedDateTime1: 2022-08-21T08:15:30+10:00[Australia/Sydney]
zonedDateTime2: 2022-08-22T08:15:30+10:00[Australia/Sydney]
zonedDateTime1 is week day: false
zonedDateTime2 is week day: true
zonedDateTime1 is weekend day: true
zonedDateTime2 is weekend day: false
Happy Coding 😊
Related Articles
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