Java code to check if date is weekend

Chapter: Date and Time Last Updated: 29-06-2023 04:45:26 UTC

Program:

            /* ............... START ............... */
                

import java.time.DayOfWeek;
import java.time.LocalDate;

public class WeekendChecker {
    public static void main(String[] args) {
        LocalDate dateToCheck = LocalDate.of(2023, 6, 30);

        if (isWeekend(dateToCheck)) {
            System.out.println(dateToCheck + " is a weekend day.");
        } else {
            System.out.println(dateToCheck + " is not a weekend day.");
        }
    }

    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
    }
}

                /* ............... END ............... */
        

Output

2023-06-30 is not a weekend day.

In this example, the code checks whether June 30, 2023, is a weekend day.
 Since June 30, 2023, falls on a Friday, the output indicates that it is not a weekend day.

Notes:

  • In this code, the isWeekend() method takes a LocalDate object as input and checks if its corresponding DayOfWeek is either Saturday or Sunday. If it is, the method returns true, indicating that the date is a weekend. Otherwise, it returns false.
  • You can change the dateToCheck value to any date you want to verify, and the code will output whether it is a weekend day or not.

Tags

Java code to check if date is weekend #Write a Java method to check if a given date is a weekend #Java program to check the given date is weekend or not

Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Add Days To Date Java Example Date and Time 08-08-2021
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1