Jordi
Jordi

Reputation: 23277

jodatime -> parse error utc datetime string

Currently, I'm trying to parse a DateTime using jodatime library.

String stringLiteral = "09/05/2016 12:25:39"
try {
  DateTime utcDateTime = new DateTime(stringLiteral, DateTimeZone.UTC);
  this.expressionType = ExpressionEnumType.date;
  this.expressions.add(ConstantImpl.create(utcDateTime.toDate()));
} catch (IllegalArgumentException e)
{
  this.expressionType = ExpressionEnumType.string;
  this.expressions.add(ConstantImpl.create(stringLiteral));
}

However, jodatime tells me:

java.lang.IllegalArgumentException: Invalid format: "09/05/2016 12:25:39" is malformed at "/05/2016 12:25:39"

Upvotes: 0

Views: 419

Answers (1)

Jesper
Jesper

Reputation: 207006

The constructor of DateTime cannot parse a string in any arbitrary format into a DateTime object. If you want to use the constructor in this way, the string must be in one of the ISO 8601 formats, and your string "09/05/2016 12:25:39" is not.

Use another method to parse the date, where you can specify the format yourself. For example:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss")
                                            .withZoneUTC();

DateTime utcDateTime = DateTime.parse(stringLiteral, formatter);

Upvotes: 1

Related Questions