calendar

public class Date {

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    public static boolean isValidDate(int date, int month, int year) {
        if (year < 1) {
            return false;
        }
        if (month < 1 || month > 12) {
            return false;
        }
        int[] daysInMonth = {31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        return date > 0 && date <= daysInMonth[month - 1];
    }

    public static String getDayOfWeek(int date, int month, int year) {
        if (month == 1 || month == 2)
{
            month += 12;
            year--;
        }

        int k = year % 100;
        int j = year / 100;

        int h = (date + 13 * (month + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;

        String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        return days[h];
    }

    public static void main(String[] args) {
        // Direct input
        int date = 21;
        int month = 7;
        int year = 2024;
        
        if (!isValidDate(date, month, year)) {
            System.out.println("Invalid date entered. Please try again.");
        }
else
{
            System.out.println("The date is: " + date + "/" + month + "/" + year);
            String dayOfWeek = getDayOfWeek(date, month, year);
            System.out.println("The day of the week is: " + dayOfWeek);
        }
    }
}

Comments

Popular posts from this blog

employee