Looks like date validation and parsing.
This can be solved using distinct approaches:
- (high-level) date-parsing functions (e.g.
java.text.DateFormat
)
- (low-level) regular-expressions (regex)
I would strongly recommend the first, because it is more exact and well-tested, plus communicates intent to peers.
Using Java's date-formats
See Parse a String to Date in Java, using SimpleDateFormat
:
String geoDate = "January; 21 2022":
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM; dd yyyy");
try {
Date date = sdf.parse(geoDate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
Note: for interpreting the month-name according the expected locale (e.g. "January" for US-en) you can specify a Locale
to the constructor: SimpleDateFormat(pattern, locale)
.
Using regex
Research for similar questions like:
Trying to improve regex for date validation
A simple regex could look like this (only matching, now capture-groups for extraction):
String geoDate = "January; 21 2022":
String regex_alphanum = "[a-zA-Z0-9 ]+"; // added a space
System.out.println(geoDate.matches(regex_alphanum)); // does not match because semicolon
String regex_alphanum_semicolon = "[a-zA-Z0-9 ;]+"; // added the semicolon
System.out.println(geoDate.matches(regex_alphanum_semicolon)); // matches
Note: the quantifier (+
) added to the character-range matches at least one or many occurrences.
Disadvantage: Even a tuned regex would probably match "Mey; 99 0021"
which is not a valid date or allow input for a date that does not exist like "February, 29 2022"
(2022 is not a leap year).