Joda Time, How to pick up format for string parsing?

Following code using Joda-Time library

Long timestamp = DateTime.parse(dateInString,DateTimeFormat.shortTime()).getMillis();

generates:

java.lang.IllegalArgumentException: Invalid format: "12.05.2014 11:42:35.808" is malformed at ".05.2014 11:42:35.808"

I tryed all DateTimeFormat.* but each format produces error.

How to fix it?

Jon Skeet
people
quotationmark

Build a DateTimeFormatter matching your pattern, and use that. Your pattern certainly isn't a "short time" pattern, given that you've got a date in there as well...

For example:

// Possibly MM.dd.yyyy - we don't know what 12.05.2014 is meant to represent
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss.SSS");
                                            .withLocale(Locale.US)
                                            .withZoneUTC(); // Adjust accordingly
DateTime dateTime = formatter.parse(text);
long millis = dateTime.getMillis();

people

See more on this question at Stackoverflow