问题
I am searching for a java library that can parse a string into a POJO without specifying the format. I have researched POjava. Is there anyother library which does similar thing?
DateTime dateTime = DateTimeParser.parse("21/02/13");
//If unclear use the cultural information passed
DateTime dateTime = DateTimeParser.parse("01/02/13", new Locale("en-us"));
//Should also work with time zones
DateTime dateTime = DateTimeParser.parse("2011/12/13T14:15:16+01:00");
I found the following links with the same problem Intelligent date / time parser for Java, but not very useful answers. Neither Joda or JChronic does what I wanted. Correct me if I am wrong.
Update:
The reason I say Joda does not solve my purpose is, Joda expects the string to be parsed in ISO8601 format or any format you specify like "yyyyMMdd". I will not be able to hardcode this format as I need to handle several formats.
I have a hack around solution for eliminating the ambiguity with respect to American or European date formats, i.e. mm/dd/yy or dd/mm/yy. Assuming I have access to timezone of the date, can I determine if it is American or European format? Can someone tell me way to do this? Googled but found nothing.
回答1:
The problem is that there are some formats that cannot be guessed right.
A simple example is 01/02/2013
. Is this february 1st or january 2nd? Or even worse: 01/02/09
?
Both formats exist. (Thank you, UK and US!)
So any format guesser will have to rely on luck for these formats, or fail deliberately for these.
The python module dateutil.parser
can serve as an example of a best effort parser. Sorry that I don't know a java equivalent. But you might want to look at Joda Time
http://labix.org/python-dateutil#head-b95ce2094d189a89f80f5ae52a05b4ab7b41af47
it actually has parameters dayfirst
and yearfirst
.
Then there is a perl module:
https://metacpan.org/pod/Time::ParseDate
You might be able to use the precedence list from that module. It's not very fast to blindly try a number of patterns (an optimized lexer will be way faster), but it may be good enough for you, unless you are guessing the format of millions of records.
回答2:
I found the answer to my problem. I used this particular library POjava. This page explains how you can format the date+time string without specifying any format. However, for the library to work properly, you got to specify the date ordering like Day followed by Month or Month followed by Day.
回答3:
There is no magic solution to this. Remember date/time formats can also depend on your locale.
Realistically the best you can do is define a list of formats, and "try" them one by one until you find one (or none) that fit.
private static final FORMAT_1 = "MM/dd/yyyy'T'HH:mm:ss.SSS"
private static final FORMAT_2 = "MM/dd/yyyy'T'HH:mm:ss"
private static final FORMAT_3 = "MM/dd/yyyy"
Remember to think about thread safety when working with date/time objects in java. I have a class doing this sort of stuff named "ThreadSafeDateTimeFormatter".
Good luck!
回答4:
Since I didn't find handy solution for my situation I wrote a simple static utility method that helped me. Wrapping formats in collection and iterating over it could make things easier if many more formats are added.
public static Date returnDateFromDateString(String propValue) throws Exception {
SimpleDateFormat sdfFormat1 = new SimpleDateFormat(IDateFConstants.DATE_STRING_FORMAT_1);
SimpleDateFormat sdfFormat2 = new SimpleDateFormat(IDateFConstants.DATE_STRING_FORMAT_2);
SimpleDateFormat sdfISO8601 = new SimpleDateFormat(IDateFConstants.DATE_STRING_ISO_8601);
try {
return sdfFormat1.parse(propValue);
} catch (ParseException e) { }
try {
return sdfFormat2.parse(propValue);
} catch (ParseException e) { }
try {
return sdfISO8601.parse(propValue);
} catch (ParseException e) { }
throw new Exception(IDateFConstants.DATE_FORMAT_ERROR);
}
where IDateFConstants
looks like
public interface IDateFConstants {
public static final String DATE_STRING_ISO_8601 = "yyyy-MM-dd'T'HH:mm:ss";
public static final String DATE_STRING_FORMAT_1 = "dd.MM.yyyy";
public static final String DATE_STRING_FORMAT_2 = "dd.MM.yyyy HH:mm:ss";
public static final String DATE_FORMAT_ERROR = "Date string wasn't" +
+ "formatted in known formats";
}
回答5:
You need to at least have an ordered list of pattern candidates. Once you have that, Apache DateUtils has a parseDate(String dateString, String[] patterns)
method that lets you easily try out a list of patterns on your date string, and parse it by the first one that matches:
public static Date parseDate(String str,
String[] parsePatterns)
throws ParseException
Parses a string representing a date by trying a variety of different parsers.
The parse will try each parse pattern in turn. A parse is only deemed successful if it parses the whole of the input string. If no parse patterns match, a ParseException is thrown.
The parser will be lenient toward the parsed date.
回答6:
public static String detectDateFormat(String inputDate, String requiredFormat) {
String tempDate = inputDate.replace("/", "").replace("-", "").replace(" ", "");
String dateFormat;
if (tempDate.matches("([0-12]{2})([0-31]{2})([0-9]{4})")) {
dateFormat = "MMddyyyy";
} else if (tempDate.matches("([0-31]{2})([0-12]{2})([0-9]{4})")) {
dateFormat = "ddMMyyyy";
} else if (tempDate.matches("([0-9]{4})([0-12]{2})([0-31]{2})")) {
dateFormat = "yyyyMMdd";
} else if (tempDate.matches("([0-9]{4})([0-31]{2})([0-12]{2})")) {
dateFormat = "yyyyddMM";
} else if (tempDate.matches("([0-31]{2})([a-z]{3})([0-9]{4})")) {
dateFormat = "ddMMMyyyy";
} else if (tempDate.matches("([a-z]{3})([0-31]{2})([0-9]{4})")) {
dateFormat = "MMMddyyyy";
} else if (tempDate.matches("([0-9]{4})([a-z]{3})([0-31]{2})")) {
dateFormat = "yyyyMMMdd";
} else if (tempDate.matches("([0-9]{4})([0-31]{2})([a-z]{3})")) {
dateFormat = "yyyyddMMM";
} else {
//add your required regex
return "";
}
try {
String formattedDate = new SimpleDateFormat(requiredFormat, Locale.ENGLISH).format(new SimpleDateFormat(dateFormat).parse(tempDate));
return formattedDate;
} catch (Exception e) {
return "";
}
}
来源:https://stackoverflow.com/questions/15010210/automatic-date-time-parser-without-specifying-format