I am trying to make a program where the user enters a date(any date), for example 29-Jul-1995. The program should match the date format and validate if this date is correct or not.
Here is my code:
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Review1_Main {
public static Pattern pattern;
public static Matcher matcher;
// DATE_PATTERN is a format for user input
private static final Pattern DATE_PATTERN = Pattern.compile("(0?[1-9]|[12][0-9]|3[01])-(^[a-zA-Z]+$)-((19|20)\\d\\d)");
// Date format validation
public static boolean dateValidation(String date) {
matcher = pattern.matcher(date);
if(matcher.matches()) {
matcher.reset();
if (matcher.find()) {
int day = 01;
String month = "Jan";
int year = 2000;
if(day == 31 && !month.equals("Apr") || !month.equals("Jun") ||
!month.equals("Sep") || !month.equals("Nov")) {
return false;
} else if(month.equals("Feb")) {
// LEAP year validation
if (year % 4 == 0) {
if (day == 30 || day == 31) {
return false;
} else {
return true;
}
} else {
if (day == 29 || day == 30 || day == 31) {
return false;
} else {
return true;
}
}// end of year % 4
} else {
return true;
}// end of day.equals
} else {
return false;
} // end of matcher.find
} else {
return false;
}// end of matcher.matcher
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String dateInput = "29-Jul-1995";
dateValidation(dateInput);
}
} // end of class
But for some reason, when I try to compile it, I always get this error:
Exception in thread "main" java.lang.NullPointerException
at Review1_Main.dateValidation(Review1_Main.java:22)
at Review1_Main.main(Review1_Main.java:73)
Where Line 22 is:
matcher = pattern.matcher(date);
and Line 73 is:
dateValidation(dateInput);
My apologize about my grammar.
Thank you.