theMadKing
theMadKing

Reputation: 2074

Scala Joda Time Format Malformed

I can't find many Scala examples with Joda time especially with formatting but here is the error:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2015-12-11T13:35:45.732-05:00" is malformed at "15-12-11T13:35:45.732-05:00"

Here is the code:

val now = DateTime.now.toString()
val dtf = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm:ss");
val timeForm = dtf.parseDateTime(now)

Most of the java exmaples all seem to work fine with this?

Upvotes: 0

Views: 2308

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170919

It has nothing to do with Scala. MM-dd-yyyy HH:mm:ss means the string will start with month, then day, etc. (see http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html for the meaning of characters in the patterns). 2015-12-11T13:35:45.732-05:00 obviously doesn't. Either use val now = dtf.printDateTime(DateTime.now) to print current datetime in the same format you are planning to parse, or use the correct format to parse it.

Upvotes: 4

Related Questions