Java Check if Today is Week Day or Weekend Day
Tags: Java Calendar DateUtil
In this Java tutorial, we learn how to check if today is a week day or weekend day in Java programming language.
How to check if today is week day or weekend day in Java
In this first step, we create a new Java class named DateUtil, and implement two methods as below.
- isTodayWeekDay() method to check if today is a week day or not.
- isTodayWeekend() method to check if today is a weekend day or not.
DateUtil.java
import java.util.Calendar;
public class DateUtil {
/**
* This method to check if today is a week day or not
* @return
*/
public static boolean isTodayWeekDay() {
Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
return (dayOfWeek != Calendar.SATURDAY)
&& (dayOfWeek != Calendar.SUNDAY);
}
/**
* This method to check if today is a weekend day or not
* @return
*/
public static boolean isTodayWeekend() {
Calendar today = Calendar.getInstance();
int dayOfWeek = today.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 the Java program to check if today is a week day or weekend day.
CheckTodayWeekDayAndWeekendExample.java
import java.util.Date;
public class CheckTodayWeekDayAndWeekendExample {
public static void main(String... args) {
// Check if Today is Week Day or Weekend Day
boolean isWeekDay = DateUtil.isTodayWeekDay();
boolean isWeekendDat = DateUtil.isTodayWeekend();
System.out.println("Today: " + new Date());
System.out.println("Today is week day: " + isWeekDay);
System.out.println("Today is weekend day: " + isWeekendDat);
}
}
Today: Wed Aug 17 23:22:36 ICT 2022
Today is week day: true
Today is weekend day: false
Happy Coding 😊
Related Articles
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
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 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 Date to Start of Day Time
Java Convert Date to End of Day Time
Java Get First Date of Current Year