Check if Date Is Weekend or Weekday in Java

In this post, we will see how to check if Date is weekend or weekday in Java.

Please note that we will consider Saturaday/Sunday as weekend and rest five days of week as weekdays.

Using LocalDate’s getDayOfWeek() method

We can use LocalDate’s getDayOfWeek() method to check if given date is weekend or weekday in Java. This method returns day from Sunday to Monday and we can use String comparison to check where day is weekend or not.

Here is java program:

Output

Using LocalDate with DayOfWeek

LocalDate’s get(ChronoField.DAY_OF_WEEK) method returns integer ranging from 1 to 7. We can get DayOfWeek from the integer using DayOfWeek's of() method and compare it with DayOfWeek.SUNDAY and DayOfWeek.SATURDAY to determine if date is weekend or weekday in Java.

1 represents Monday and 7 represents Sunday.

Output

Using Date and Calendar classes

We can convert Date to Calenday object and use Calendar.get(Calendar.DAY_OF_WEEK) to get integer which represents day of the week and compare it with Calendar.SATURDAY or Calendar.SUNDAY to check if given date is weekend or weekday.

In case of Calendar, 1 represents Sunday and 7 represents Saturday.

Output

That’s all about how to check if date is weekend or weekday in Java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *