Nutel
Nutel

Reputation: 2304

Malformed timezone in JodaTime

I have the following snippet that uses Joda time:

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("HH z")
String dateTime = dateTimeFormatter.print(new DateTime())
println DateTime.parse(dateTime, dateTimeFormatter)

But it throws the IllegalArgumentException:

Invalid format: "16 EDT" is malformed at "EDT"

What do I miss?

EDIT: Works great for DateTimeFormat.forPattern("HH")

Upvotes: 1

Views: 2711

Answers (2)

boneill
boneill

Reputation: 1526

The problem is that EDT is not a valid unique time zone name. The parser is very strict in this regard. EDT can apply to the United States or Australia.

Upvotes: 0

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15219

According to the DateTimeFormat javadoc, time zone names ('z') cannot be parsed.

EDIT: To parse a timezone, I'd look into using 'Z' and the actual time zone offset (eg. -0500 for Eastern). Also there's the withOffsetParsed() method in DateTimeFormatter you may want to look into -- eg. dateTimeFormatter.withOffsetParsed().parseDateTime("16 -0500");.

Upvotes: 3

Related Questions