Write a windowing Java program (using JOptionPane) that prompts the user for a d
ID: 3564192 • Letter: W
Question
Write a windowing Java program (using JOptionPane) that prompts the user for a date. Your class should be called TestDate.
The program should read the string from the keyboard and test whether it contains a VALID date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. Use appropriate dialogs to both get the input and display the output.
The input date should have the format mm/dd/yyyy or mm-dd-yyyy (The numbers may be separated by either slashes or hyphens.) A valid month value, mm, must be from 01 to 12 (January is 01). The day value, dd, must be from 1 to a value that 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. (Divisible should be interpreted as EVENLY divisible with no remainder.)
Carefully design your own test data which will test every possible type of valid and invalid month, day, and year. Start by making a list of invalid dates and noting WHY they are invalid. This should help you to design the decision statements in your program.
Thanks in advance!
Explanation / Answer
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class TestDate
{
public static void main(String[] args)
{
String date=JOptionPane.showInputDialog("Enter the date");
date=date.trim(); // mm/dd/yyyy or mm-dd-yyyy
check(date);
}
public static void check(String date)
{
int a[]=new int[3];
StringTokenizer st=new StringTokenizer(date,"/-");
if(date.indexOf("/")>0 && date.indexOf("-")>0){ // mixed combo of /,- is an error
System.out.println(date+" Not Valid: use either / or - ."); return; }
else
for(int i=0;st.hasMoreTokens();i++) // storing values in array
a[i]=Integer.parseInt(st.nextToken());
if(a[0]>12 || a[0]<1) { // checking if month exceeds 12
System.out.println(date+" Not Valid: Month has to b/w 1 and 12 inclusive.");
return;
}
if(a[1]>31 || a[0]<1) { // checking if date exceeds 31
System.out.println(date+" Not Valid: date has to b/w 1 and 31 inclusive.");
return;
}
String month[]={"January","February","March","April","May","June","July","August","September","October","Novermber","December"};
boolean monthCheck= a[0]==4 || a[0]== 6|| a[0]==9 || a[0]==11;
if(monthCheck && a[1]==31){ // checking if for specific months date exceeds 30
System.out.println(date+" Not Valid: "+month[a[0]-1]+" month has only 30 days");
return;
}
boolean yea=false;
yea=a[2]%400==0 || (a[2]%4==0 && a[2]%100!=0 );
if(!yea && a[0]==2 && a[1]>28){ // not leap year, feb, date exceeds 28
System.out.println(date+" Not valid: In non leap year ("+a[2]+"), February has only 28 days"); return; }
else if (yea && a[0]==2 && a[1]>29) {// leap year, feb, date exceeds 29
System.out.println(date+" Not Valid: In leap year ("+a[2]+"), February has only 29 days"); return; }
System.out.println("Date ("+date+") is correct");
}
}
output:
13/2/345 Not Valid: Month has to b/w 1 and 12 inclusive.
12/32/2014 Not Valid: date has to b/w 1 and 31 inclusive.
2/3-2034 Not Valid: use either / or - .
4/31/2014 Not Valid: April month has only 30 days
2/29/2013 Not valid: In non leap year (2013), February has only 28 days
2/30/2000 Not Valid: In leap year (2000), February has only 29 days
Date (2/29/2000) is correct
Date (2/2/2000) is correct
Date (2/28/2000) is correct
Date (3/29/2034) is correct
Date (4/23/2300) is correct
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.