cuh
cuh

Reputation: 3800

What is the correct pattern for parsing the timezone format with SimpleDateFormat

I want to define a pattern for the Java SimpleDaterFormat to parse existing strings.

The existing dates look like this: 2011-05-02T13:40:00+02:00.

I tried with different patterns, but I got ParseExceptions. The problem seems to be the timezone format.

Printing the pattern in Java:

But how can I get

I'm using Java 6, not Java 7.

Upvotes: 6

Views: 3402

Answers (2)

Augustin Ghauratto
Augustin Ghauratto

Reputation: 1520

I know it's a bit old question, but someone else might benefit from my hint. You can use JodaTime. As library documentation stands:

Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.

You can use it as well with java 6. You have more examples in this question

Upvotes: 1

matts
matts

Reputation: 6887

If you can use Java 7 or newer, you can use the XXX pattern to get the timezone to look like +02:00:

yyyy-MM-dd'T'HH:mm:ssXXX

Otherwise you might have to manipulate the date string to remove the colon from the timezone before parsing it.

Upvotes: 6

Related Questions