I have a string in the format of:
3:00 pm on Aug 28
What would be the best way to verify that a valid time and valid date is contained within this string? My first thought was to split the string and use two regexs to match a time and the other one to match that specfic date format (abbreviate month day). However I'm having a little bit of trouble with the second regex (the one for the specfic date format). How else could one go about verifying the string is in the correct format?
解决方案
You can try this:
public boolean isValid( String dateStr ) {
// K: hour of the day in am/pm
// m: minute of a hour
// 'on': static text
// MMM: name of the month with tree letters
// dd: day of the month (you can use just d too)
DateFormat df = new SimpleDateFormat( "K:m a 'on' MMM dd", Locale.US );
try {
df.parse( dateStr );
return true;
} catch ( ParseException exc ) {
}
return false;
}