×

Java Programs

Java Practice

Java program to validate date and print weekday of the given date

Given/input a date, we have to validate date and print weekday of the given date.
Submitted by Nidhi, on March 03, 2022

Problem statement

In this program, we will read a day from the user and find the weekday corresponding given date, and print the result.

Source Code

The source code to validate the date and print weekday of the given date is given below. The given program is compiled and executed successfully.

// 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");
    }
  }
}

Output

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

Explanation

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains three static methods validateDate(), wd(), and main().

The validateDate() method is used to check given date is valid or not. The wd() method is used to return the week number based on the given date.

The main() method is an entry point for the program. Here, we read a date from the user using the Scanner class. Then we got the corresponding weekday.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.