Write a method called validateDate that takes a single String argument and check
ID: 3693198 • Letter: W
Question
Write a method called validateDate that takes a single String argument and checks whether this String is a valid date in the form mm/dd/yyyy. Your method should throw an InvalidDateException if the date is not valid.
• There could be dierent cases which makes the date invalid, make sure to account for all the cases. Assume all months have 31 days.
• To extract month, day and year components from the single String parameter, you can use the following method of the String class substring(b, e): extracts and returns the substring from position b through position e-1. Generates an IndexOutOfBoundsException if b or e-1 is illegal.
• Recall also that Integer.parseInt(...) can be used to convert a String to an int, and this method can possibly throw a NumberFormatException.
• Do NOT catch InvalidDateException inside the method.
Explanation / Answer
DateFormatChecker.java
import java.util.Scanner;
public class DateFormatChecker {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the date in the format (mm/dd/yyyy)");
String date = scan.next();
validateDate(date);
}
catch(InvalidDateException e){
System.out.println(e);
}
}
public static void validateDate(String date) throws InvalidDateException{
int month = Integer.parseInt(date.substring(0, 2));
int day = Integer.parseInt(date.substring(3, 5));
int year = Integer.parseInt(date.substring(6));
if(month < 1 || month > 12){
throw new InvalidDateException("Invalid Month in Given Date");
}
if(date.substring(6).length() != 4 ){
throw new InvalidDateException("Invalid year in Given Date. Year should be a four digit number");
}
if((month ==1 || month == 3 || month ==5 || month == 7 || month ==8 || month == 10 || month == 12) && (day < 1 || day > 31) ){
throw new InvalidDateException("Invalid day in Given Date. We have 31 days in that particulat month starts from 1 to 31");
}
else if((month == 4 || month == 6 || month == 9 || month ==11) && (day < 1 || day > 30)){
throw new InvalidDateException("Invalid day in Given Date. We have 30 days in that particulat month starts from 1 to 30");
}
else if(month == 2 && (day < 1 || day > 28) && year % 4 != 0){
throw new InvalidDateException("Invalid day in Given Date. We have 28 days in that particulat month starts from 1 to 28");
}
else if(month == 2 && (day < 1 || day > 29) && year % 4 == 0){
throw new InvalidDateException("Invalid day in Given Date. We have 29 days in that particulat month starts from 1 to 29");
}
}
}
InvalidDateException.java
public class InvalidDateException extends Exception{
String str1;
public InvalidDateException(){
}
public InvalidDateException (String s){
this.str1 = s;
}
public String toString(){
return (str1) ;
}
}
Output:
Please enter the date in the format (mm/dd/yyyy)
13/12/2011
Invalid Month in Given Date
Please enter the date in the format (mm/dd/yyyy)
12/12/201
Invalid year in Given Date. Year should be a four digit number
Please enter the date in the format (mm/dd/yyyy)
11/31/2011
Invalid day in Given Date. We have 30 days in that particulat month starts from 1 to 30
Please enter the date in the format (mm/dd/yyyy)
02/30/2016
Invalid day in Given Date. We have 29 days in that particulat month starts from 1 to 29
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.