Java Check if LocalDateTime is Week Day or Weekend Day
Tags: Java LocalDateTime DayOfWeek DateUtil
In this Java tutorial, we learn how to check if a given LocalDateTime value is a week day or weekend day in Java programming language.
How to check if LocalDateTime object is week day or weekend day in Java
In this first step, we create a new Java class named DateUtil, and implement two static methods below.
- isWeekDay(LocalDateTime localDateTime) static method to check if a given LocalDateTime object is a week day or not.
- isWeekendDay(LocalDateTime localDateTime) static method to check if a given LocalDateTime object is a weekend day or not.
DateUtil.java
import java.time.DayOfWeek;
import java.time.LocalDateTime;
public class DateUtil {
/**
* This method to check if a LocalDateTime value is a week day or not.
* @param localDateTime
* @return
*/
public static boolean isWeekDay(LocalDateTime localDateTime) {
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
return (dayOfWeek != DayOfWeek.SATURDAY)
&& (dayOfWeek != DayOfWeek.SUNDAY);
}
/**
* This method to check if a LocalDateTime value is a weekend day or not.
* @param localDateTime
* @return
*/
public static boolean isWeekendDay(LocalDateTime localDateTime) {
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
return (dayOfWeek == DayOfWeek.SATURDAY)
|| (dayOfWeek == DayOfWeek.SUNDAY);
}
}
In the following example Java code, we learn how to use the static methods from first step to check LocalDateTime values whether it is a week day or weekend day in Java program.
CheckLocalDateTimeWeekDayAndWeekendExample.java
import java.time.LocalDateTime;
public class CheckLocalDateTimeWeekDayAndWeekendExample {
public static void main(String... args) {
LocalDateTime localDateTime1 = LocalDateTime.of(2022, 8, 19, 0, 0, 0);
LocalDateTime localDateTime2 = LocalDateTime.of(2022, 8, 20, 0, 0, 0);
// Check if an LocalDateTime is Week Day or not
boolean isWeekDay1 = DateUtil.isWeekDay(localDateTime1);
boolean isWeekDay2 = DateUtil.isWeekDay(localDateTime2);
// Check if an LocalDateTime is Weekend Day or not
boolean isWeekend1 = DateUtil.isWeekendDay(localDateTime1);
boolean isWeekend2 = DateUtil.isWeekendDay(localDateTime2);
System.out.println("localDateTime1: " + localDateTime1);
System.out.println("localDateTime2: " + localDateTime2);
System.out.println("\nlocalDateTime1 is week day: " + isWeekDay1);
System.out.println("localDateTime2 is week day: " + isWeekDay2);
System.out.println("\nlocalDateTime1 is weekend: " + isWeekend1);
System.out.println("localDateTime2 is weekend: " + isWeekend2);
}
}
localDateTime1: 2022-08-19T00:00
localDateTime2: 2022-08-20T00:00
localDateTime1 is week day: true
localDateTime2 is week day: false
localDateTime1 is weekend: false
localDateTime2 is weekend: true
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