ADVERTISEMENT
ADVERTISEMENT

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 Solution:

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

Program/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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.