Java Program to Validate Date and Print Weekday of the Given Date

Java Program to Validate Date and Print Weekday of the Given Date

Java Program to Validate Date and Print Weekday of the Given Date

// Java program to find the number of days in a month 
// using a switch statement

import java.util.Scanner;

public class Main {

  static int validateDate(int d, int m, int y) {
    if (y >= 1800 && y <= 2999) {
      if (m >= 1 && m <= 12) {
        if (d >= 1 && d <= 31) {
          if ((d >= 1 && d <= 30) && (m == 4 || m == 6 || m == 9 || m == 11))
            return 1; //valid date
          else if ((d >= 1 && d <= 30) && (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12))
            return 1; //valid date
          else if ((d >= 1 && d <= 28) && (m == 2))
            return 1; //valid date
          else if (d == 29 && m == 2 && ((y % 400 == 0) || (y % 4 == 0 && y % 4 != 0)))
            return 1; //valid date
          else
            return 0; //invalid day
        } else {
          return 0; //day is invalid
        }
      } else {
        return 0; //month is invalid
      }
    } else {
      return 0; //year is invalid
    }
  }

  static int wd(int year, int month, int day) {
    int wday = 0;
    wday = (day + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) +
        (365 * (year + 4800 - ((14 - month) / 12))) +
        ((year + 4800 - ((14 - month) / 12)) / 4) -
        ((year + 4800 - ((14 - month) / 12)) / 100) +
        ((year + 4800 - ((14 - month) / 12)) / 400) -
        32045) %
      7;
    return wday;
  }

  public static void main(String[] args) {
    Scanner SN = new Scanner(System.in);

    int day, month, year;
    int wDayNo = 0;
    String dayNames[] = new String[] {
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
      "Sunday"
    };

    System.out.printf("Input date: \n");

    System.out.printf("\tEnter day(DD): ");
    day = SN.nextInt();

    System.out.printf("\tEnter month(MM): ");
    month = SN.nextInt();

    System.out.printf("\tEnter year(YYYY): ");
    year = SN.nextInt();

    if (validateDate(day, month, year) == 1) {
      System.out.printf("Date is correct [%02d/%02d/%02d].\n", day, month, year);

      wDayNo = wd(year, month, day);

      System.out.printf("week day is: %s\n", dayNames[wDayNo]);
    } else {
      System.out.printf("Date is in-correct.\n");
    }
  }
}
RUN 1:
Input date: 
        Enter day(DD): 26
        Enter month(MM): 2
        Enter year(YYYY): 2022
Date is correct [26/02/2022].
week day is: Saturday

RUN 2:
Input date: 
        Enter day(DD): 29
        Enter month(MM): 2
        Enter year(YYYY): 2022
Date is in-correct.

RUN 3:
Input date: 
        Enter day(DD): 10
        Enter month(MM): 7
        Enter year(YYYY): 1988
Date is correct [10/07/1988].
week day is: Sunday

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment