Java 8 – How to check whether a given Date is weekend ?

In this article, we will learn how to check whether a given Date is weekend or not ?

Check given Date is weekend :

  • Using LocalDate
  • Using Date/Calendar

Assumption: Sunday & Saturday are weekends

1. LocalDate – check given date is weekend :

  • First, get Day of Week for the invoking LocalDate using LocalDate.get(ChronoField.DAY_OF_WEEK) method which returns integer value from 1 to 7 with,
    • 1 being Monday
    • 2 being Tuesday
    • 6 being Saturday
    • 7 being Sunday
  • Create separate method to check if the invoking LocalDate‘s Day of Week value –
    1. If it is 6 or 7, then it is weekends
    2. otherwise it is weekdays
  • Finally, print result to the console

CheckWeekendForLocalDate.java

package in.bench.resources.java8.current.date.time;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;

public class CheckWeekendForLocalDate {

	// main() method
	public static void main(String[] args) {

		// 1. get Current LocalDate
		LocalDate localDate = LocalDate.now();
		System.out.println("Current Day is :- " + localDate);
		System.out.println("Whether (" + localDate + ") is weekend ? :- " 
				+ isWeekend(localDate));


		// 2. get Past LocalDate
		LocalDate localDatePast = LocalDate.of(2022, Month.SEPTEMBER, 10);
		System.out.println("\nPast Day is :- " + localDatePast);
		System.out.println("Whether (" + localDatePast + ") is weekend ? :- " 
				+ isWeekend(localDatePast));


		// 1. get Future LocalDate
		LocalDate localDateFuture = LocalDate.of(2022, Month.SEPTEMBER, 18);
		System.out.println("\nFuture Day is :- " + localDateFuture);
		System.out.print("Whether (" + localDateFuture + ") is weekend ? :- " 
				+ isWeekend(localDateFuture));
	}


	/**
	 * This method checks or verifies whether passed LocalDate is weekend or not considering
	 * 
	 * - SATURDAY is 6
	 * - SUNDAY is 7
	 * 
	 * @param localDate
	 * @return
	 */
	private static boolean isWeekend(LocalDate localDate) {

		// get Day of week for the passed LocalDate
		return (localDate.get(ChronoField.DAY_OF_WEEK) == 6) 
				|| (localDate.get(ChronoField.DAY_OF_WEEK) == 7);
	}
}

Output:

Current Day is :- 2022-09-14
Whether (2022-09-14) is weekend ? :- false

Past Day is :- 2022-09-10
Whether (2022-09-10) is weekend ? :- true

Future Day is :- 2022-09-18
Whether (2022-09-18) is weekend ? :- true

2. Date/Calendar – check given date is weekend :

  • First, get Day of Week for the invoking Date using Calendar.get(Calendar.DAY_OF_WEEK) method which returns integer value from 1 to 7 with,
    • 1 being Sunday
    • 2 being Monday
    • ….
    • 7 being Saturday
  • Create separate method to check if the invoking Date‘s Day of Week value –
    1. If it is 1 or 7, then it is weekends
    2. otherwise it is weekdays
  • Finally, print result to the console

CheckWeekendForDate.java

package in.bench.resources.java8.current.date.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CheckWeekendForDate {

	// main() method
	public static void main(String[] args) throws ParseException {

		// Date formatter
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");


		// 1. get Current Date/time
		Date current = new Date();
		System.out.println("Current Day date/time is :- " + current);
		System.out.println("Whether (" + current + ") is weekend ? :- " 
				+ isWeekend(current));


		// 2. get Future Date/time
		String futureDateStr = "18-09-2022";
		Date futureDate = dateFormat.parse(futureDateStr);
		System.out.println("\nFuture Day date/time is :- " + futureDate);
		System.out.println("Whether (" + futureDate + ") is weekend ? :- " 
				+ isWeekend(futureDate));


		// 3. get Past Date/time
		String pastDateStr = "10-09-2022";
		Date pastDate = dateFormat.parse(pastDateStr);
		System.out.println("\nPast Day date/time is :- " + pastDate);
		System.out.print("Whether (" + pastDate + ") is weekend ? :- " 
				+ isWeekend(pastDate));
	}


	/**
	 * This method checks or verifies whether passed Date is weekend or not considering
	 * 
	 * - SUNDAY is 1
	 * - SATURDAY is 7
	 * 
	 * @param date
	 * @return
	 */
	private static boolean isWeekend(Date date) {

		// get Calendar instance
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);


		// get Day of week for the passed LocalDate
		return (calendar.get(Calendar.DAY_OF_WEEK) == 1) 
				|| (calendar.get(Calendar.DAY_OF_WEEK) == 7);
	}
}

Output:

Current Day date/time is :- Tue Sep 13 20:33:17 IST 2022
Whether (Tue Sep 13 20:33:17 IST 2022) is weekend ? :- false

Future Day date/time is :- Sun Sep 18 00:00:00 IST 2022
Whether (Sun Sep 18 00:00:00 IST 2022) is weekend ? :- true

Past Day date/time is :- Sat Sep 10 00:00:00 IST 2022
Whether (Sat Sep 10 00:00:00 IST 2022) is weekend ? :- true

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a number exists in an Arrays or List or Stream ?
Java 8 – How to get next and previous date ?