问题
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.
回答1:
You are initialising pattern
in the constructor but using it in a static method dateValidation
. Unless you have constructed an object using new
before calling the method then the static field will not be initialised (hence the null pointer exception).
The solution is to turn your pattern into a constant rather than compiling it in the constructor:
private static final Pattern DATE_PATTERN = Pattern.compile("...");
And then use the constant (rather than pattern
) to create the Matcher
:
Matcher matcher = DATE_PATTERN.match(dateInput);
You don't need the pattern
or matcher
static variables.
Note that there's really no reason for you to be using a static method. Better would be to call this a DateValidator
class and make the validation method an instance method. The pattern can still be a constant.
回答2:
With Java 8:
String dateInput = "01-Feb-2016";
List<String> months = new ArrayList<String>();
months.add("Jan");
months.add("Feb");
months.add("Mar");
months.add("Apr");
months.add("May");
months.add("Jun");
months.add("Jul");
months.add("Aug");
months.add("Sep");
months.add("Oct");
months.add("Nov");
months.add("Dec");
boolean dateIsValid = true;
try
{
String [] vals = dateInput.split("-");
int day = Integer.parseInt(vals[0]);
int mon = months.indexOf(vals[1]) + 1;
int year = Integer.parseInt(vals[2]);
LocalDate.of(year, mon, day);
}
catch (DateTimeException e)
{
dateIsValid = false;
}
System.out.println("Date is valid: " + dateIsValid);
来源:https://stackoverflow.com/questions/37403354/regex-calendar-validation