Table of Contents
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; import java.time.LocalDate; public class CheckWeekendMain { public static void main(String[] args) { LocalDate ld1=LocalDate.of(2022,9,17); boolean isWeekend1 = isWeekEnd(ld1); System.out.println("17 Sep 2022 isWeekend: "+isWeekend1); LocalDate ld2=LocalDate.of(2022,9,19); boolean isWeekend2 = isWeekEnd(ld2); System.out.println("19 Sep 2022 isWeekend: "+isWeekend2); } /*Function to check if given date is weekend or not*/ public static boolean isWeekEnd(LocalDate localDate) { String dayOfWeek = localDate.getDayOfWeek().toString(); if("SATURDAY".equalsIgnoreCase(dayOfWeek)|| "SUNDAY".equalsIgnoreCase(dayOfWeek)) { return true; } return false; } } |
Output
1 2 3 4 |
17 Sep 2022 isWeekend: true 19 Sep 2022 isWeekend: false |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.ChronoField; public class CheckWeekendMain { public static void main(String[] args) { LocalDate ld1=LocalDate.of(2022,9,17); boolean isWeekend1 = isWeekEnd(ld1); System.out.println("17 Sep 2022 isWeekend: "+isWeekend1); LocalDate ld2=LocalDate.of(2022,9,19); boolean isWeekend2 = isWeekEnd(ld2); System.out.println("19 Sep 2022 isWeekend: "+isWeekend2); } /*Function to check if given date is weekend or not*/ public static boolean isWeekEnd(LocalDate localDate) { DayOfWeek dayOfWeek = DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK)); if(DayOfWeek.SUNDAY == dayOfWeek|| DayOfWeek.SATURDAY == dayOfWeek) { return true; } return false; } } |
Output
1 2 3 4 |
17 Sep 2022 isWeekend: true 19 Sep 2022 isWeekend: false |
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; import java.util.Calendar; import java.util.Date; public class CheckWeekEndCalendarMain { public static void main(String[] args) { Date d1=new Date(2022,8,17); boolean isWeekend1 = isWeekEnd(d1); System.out.println("17 Sep 2022 isWeekend: "+isWeekend1); Date d2=new Date(2022,8,19); boolean isWeekend2 = isWeekEnd(d2); System.out.println("19 Sep 2022 isWeekend: "+isWeekend2); } /* Function to check if given date is weekend or not */ public static boolean isWeekEnd(final Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int day = cal.get(Calendar.DAY_OF_WEEK); return day == Calendar.SUNDAY || day == Calendar.SATURDAY; } } |
Output
1 2 3 4 |
17 Sep 2022 isWeekend: true 19 Sep 2022 isWeekend: false |
That’s all about how to check if date is weekend or weekday in Java.