Automatic date / time parser without format

I am looking for a java library that can parse a string in POJO without specifying a format. I researched POjava . Is there any other library that does this?

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 nor Ya Chronik do what I wanted. Correct me if I am wrong.

Update:

The reason I say that Joda does not solve my goal, Joda expects the string to be parsed in the ISO8601 format or in any format that you specify as "yyyyMMdd". I will not hard code this format, since I need to handle several formats.

I have a solution to disambiguate regarding date formats in the USA or Europe, i.e. mm / dd / yy or dd / mm / yy. Assuming I have access to the time zone of a date, can I determine if this is an American or European format? Can someone tell me how to do this? Googled, but found nothing.

+4
source share
4 answers

The problem is that there are some formats that cannot be guessed correctly.

A simple example is 01/02/2013 . Is it February 1 or January 2? Or worse: 01/02/09 ?

Both formats . (Thank you, UK and USA!)

Thus, any format guesser will have to rely on luck for these formats or not intentionally do so.

The python dateutil.parser can serve as an example of the best parsing. Sorry, I do not know the equivalent of java. But you can watch Joda Time

http://labix.org/python-dateutil#head-b95ce2094d189a89f80f5ae52a05b4ab7b41af47

it really has dayfirst and yearfirst .

Then there is the perl module:

https://metacpan.org/pod/Time::ParseDate

You may be able to use the priority list from this module. It is not very fast to blindly try several templates (an optimized lexer will be faster), but it can be good enough for you if you do not guess the format of millions of records.

+8
source

There is no magic solution. Remember that date and time formats also depend on your language.

Realistically, the best thing you can do is define a list of formats and try them one by one until you find one (or nothing) that works.

 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" 

Do not forget to think about thread safety when working with date / time objects in java. I have a class doing such things called "ThreadSafeDateTimeFormatter".

Good luck

0
source

Since I did not find a convenient solution for my situation, I wrote a simple static method that helped me. Formatting formats in a collection and repeating them can make it easier if you add many more formats.

 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"; } 
0
source

All Articles