Write a Java application program that will allow the user to enter a date. The program will
Question:
Write a Java application program that will allow the user to enter a date. The program will then check the date to verify whether or not it is a valid date.
Begin by asking the user to enter a date in the form, mm/dd/yyyy. This will be entered as a String, since the date contains slash marks.
After the date is entered, the program should check the date for validity. If the date is valid, the program should display the message "Valid date". If the date is not valid, the program should display the message "Not a valid date" along with a message to indicate why the date is not valid.
To check the date:
Begin by checking to make sure the user has entered enough characters. A correct date should consist of exactly 10 characters - no more and no less. If the user has not entered the correct number of characters, display whichever of the following messages is appropriate:
Too few characters in the date
Too many characters in the date
If the first test turned out to be ok, next you should check to make sure that the slash marks are in the correct positions. In a correctly entered date, a slash mark should be the third character in the string, and also the sixth character in the string. If you do not find slash marks at these two points, display the following message:
Incorrect format
If the first two tests turned out to be ok, next you should check the month. You will need to extract the month part from the string. Since it will be extracted as a string, you will then need to convert it to an integer so you can check it's value. A valid month value must be from 1 to 12. If the month is not in the proper range, display the following message:
Month is not valid
If the first three tests turned out to be ok, next check the day. You will need to extract the day part from the string. Since it will be extracted as a string, you will then need to convert it to an integer so you can check it's value. The day value should not be less than 1. It should also not be greater than 28, 29, 30 or 31; whichever is appropriate for the given month. September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29. The remaining months all have 31 days each. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400. If the day is not in the proper range, display the following message:
Day is not valid
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics:
Enter a date (mm/dd/yyyy): 123/456/7890
Not a valid date.
Too many characters in the date
Here is another example program run:
Enter a date (mm/dd/yyyy): 12/456/789
Not a valid date.
Incorrect format
Here is another example program run:
Enter a date (mm/dd/yyyy): 13/15/1929
Not a valid date.
Month is not valid
Here is another example program run:
Enter a date (mm/dd/yyyy): 06/31/2009
Not a valid date.
Day is not valid
Here is another example program run:
Enter a date (mm/dd/yyyy): 02/29/2009
Not a valid date.
Day is not valid
Here is another example program run:
Enter a date (mm/dd/yyyy): 02/29/2008
Valid date.
Hints:
You may assume that the user will only enter numbers, and the slash marks, and there will not be more than two slash marks.
To convert a String to an integer, use the Integer.parseInt method. To use it, you put a String in the parentheses, and it returns to you the integer equivalent of the string. For example: String x = "123"; int y = Integer.parseInt(x); After the above two lines execute, the variable "y" will be holding the integer 123.Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang