问题
My problem is not "how to do it", my problem is the corner cases involved. Here is my code and test case.
package datetest.com;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class MultipleDateParserFormat {
private final static String[] PossibleDateFormat=new String[]{
"MM/dd/yyyy",
"MM.dd.yyyy",
"MM-dd-yyyy",
"yyyy/MM/dd",
"yyyy.MM.dd",
"yyyy-MM-dd",
"dd/MM/yyyy",
"dd.MM.yyyy",
"dd-MM-yyyy"
};
public MultipleDateParserFormat(){};
public void checkParseDate(String passedDate){
int PossibleDateFormatLength=PossibleDateFormat.length;
for(int i=0;i<PossibleDateFormatLength;i++){
try {
SimpleDateFormat simpleDateFormant=new SimpleDateFormat(PossibleDateFormat[i]);
simpleDateFormant.setLenient(false);
java.util.Date date= simpleDateFormant.parse(passedDate);
System.out.println(date);
} catch (ParseException e) {
System.out.println("Parse Exception Occured for your input Value"+passedDate + "for format" + PossibleDateFormat[i]);
}
}
}
public static void main(String...strings){
String passedDate="4-03-1992";// This test case is validating against two Pattern
MultipleDateParserFormat multipleDateParserFormat= new MultipleDateParserFormat();
multipleDateParserFormat.checkParseDate(passedDate);
}
}
Here is the OUTPUT that I am getting when I run this code:
Parse Exception Occured for your input Value4-03-1992for formatMM/dd/yyyy
Parse Exception Occured for your input Value4-03-1992for formatMM.dd.yyyy
Fri Apr 03 00:00:00 IST 1992
Parse Exception Occured for your input Value4-03-1992for formatyyyy/MM/dd
Parse Exception Occured for your input Value4-03-1992for formatyyyy.MM.dd
Parse Exception Occured for your input Value4-03-1992for formatyyyy-MM-dd
Parse Exception Occured for your input Value4-03-1992for formatdd/MM/yyyy
Parse Exception Occured for your input Value4-03-1992for formatdd.MM.yyyy
Wed Mar 04 00:00:00 IST 1992
Since my test case is satisfying two given patterns, I'm getting two outputs.
How can I avoid such cases here? What are other ways to do such multiple pattern validation?
回答1:
I think the only reliable way is to know date format in advance. You can never know if 01/01/2014 is dd/MM/yyyy or MM/dd/yyyy
回答2:
Patterns MM-dd-yyyy and dd-MM-yyyy are both valid for 4-03-1992. That is why you get 2 results that pass. If you want to test to only allow one through then set one field to >12.
As for another way - there are many different ways. Personally I wouldn't want to rely on exceptions being thrown. Look in to the Regex API in Java:
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
You can figure it out from there.
回答3:
Seems you need some sort of simple classifier:
- Take big set of training texts.
- Process them, counting and remembering frequencies for every pattern.
Then, if multiple matches are found, take one of pattern with greater frequency.
It won't give 100% accuracy, but will significantly improve it. It is important to take training data from sources close to that you will use and for the same language/region. For example, in Russia we never use mm/dd/yyyy
nor mm.dd.yy
, but for US it is a very common format.
来源:https://stackoverflow.com/questions/29751244/how-to-validate-date-against-multiple-patterns-format-in-java