Question
determine if a date entered by the user is valid. The user is to enter a date in this format: mm/dd/yyyy where mm represents the
determine if a date entered by the user is valid.
The user is to enter a date in this format:
mm/dd/yyyy
where mm represents the month, dd represents the day and yyyy represents the year. Your code MUST accept the date as a single string.
Your code must print to the screen if the date is or is not valid. If the date is not valid you are to print the reason. A date entered may be invalid because:
- invalid length: the string must be 10 characters in length
- invalid format: missing / or / in incorrect position(s)
- invalid month: must be 1 - 12
- invalid day: must be 1 - 30, 1 - 31, 1 - 28, 1- 29 depending on month and year (in a leap year February has 29 days)
- invalid year: must be >= 0 and <10000
You may assume that the user entered a string consisting of digits (0-9) and forward slashes (/). You do not have to deal with the user entering letters or symbols.
Hint: to convert a String to a number we use the parseInt method of the Integer class. For example if we have:
String year = "12";
we can convert it to a number with this code (I will use the variable name yearInt to make it easier to remember):
int yearInt = Integer.parseInt(year);
Hint: a leap year is a year that is evenly divisible by 4, if the year is evenly divisible by 100 it must also be evenly divisible by 400, for example the year 2000 is a leap year, the year 1900 is not. Here is some code you could use to determine if a year is a leap year
int yearInt: // in this variable you store the value of the year entered
boolean leap; //declare a boolean
leap = ((yearInt % 4 == 0) && (yearInt % 100 != 0)) ||
(yearInt % 400 == 0); //this returns true or false
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres the Java code to validate the date Java import javautilScanner public class DateValidator publ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started