What is the correct template for parsing timezone format using SimpleDateFormat

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

Existing dates are as follows: 2011-05-02T13:40:00+02:00 .

I tried with different templates, but I got ParseExceptions. The problem is the time zone format.

Printing a template in Java:

  • yyyy-MM-dd'T'HH:mm:ssZ
    • 2012-03-14T15:40:44+0100
  • yyyy-MM-dd'T'HH:mm:ssZ
    • 2012-03-14T15:41:58MEZ

But how can I get

  • ???
    • 2011-05-02T13:40:00+02:00

I am using Java 6 and not Java 7.

+6
source share
2 answers

If you can use Java 7 or later, you can use the XXX pattern to get the time zone so that it looks like +02: 00:

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

Otherwise, you may need to manipulate the date string to remove the colon from the time zone before it is parsed.

+5
source

I know this is a little old question, but someone might benefit from my hint. You can use JodaTime . The documentation library is:

Zone: “Z” displays the offset without a colon, “ZZ” displays the offset with a colon, “ZZZ” or more displays the zone identifier.

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

0
source

Source: https://habr.com/ru/post/910715/


All Articles