Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX

48,077

Solution 1

use this format yyyy-MM-dd'T'HH:mm:ss.SSSSX

From SimpleDateFormat API

//Letter    Date or Time Component  Presentation        Example
  S         Millisecond             Number              978
  X         Time zone               ISO 8601 time zone  -08; -0800; -08:00

USE:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSX");
String date = "2014-12-03T10:05:59.5646+08:00";
System.out.println(format.parse(date));

OUTPUT:

Wed Dec 03 03:06:04 CET 2014

Solution 2

Those are valid formats:

yyyy-MM-dd'T'HH:mm:ss.SSSZ       >>>  e.g.: 2001-07-04T12:08:56.235-0700

yyyy-MM-dd'T'HH:mm:ss.SSSXXX     >>>  e.g.: 2001-07-04T12:08:56.235-07:00

Edit:
BTW, "X" refer to the (ISO 8601 time zone)

Share:
48,077
praveen_mohan
Author by

praveen_mohan

Updated on July 16, 2022

Comments

  • praveen_mohan
    praveen_mohan almost 2 years

    I am trying to parse a date 2014-12-03T10:05:59.5646+08:00 using these two formats:

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

    When I parse using yyyy-MM-dd'T'HH:mm:ss it works fine, but when I parse yyyy-MM-dd'T'HH:mm:ssXXX a ParseException is thrown.

    Which is the correct format to parse the date and also what exactly is the difference between these two formats?

    Note : I cannot use Joda :(

  • Michael-O
    Michael-O over 8 years
    the first example is not valid since the tz offset is not extended.
  • Michael-O
    Michael-O over 8 years
    Don't use that format. Read my comment to the first answer.
  • Jordi Castilla
    Jordi Castilla over 8 years
    @Michael-O a) actually first answer is mine. b) this code is working, please check here a working demo c) can you please explain what you mean with example is not valid since the tz offset is not extended ?? thanks
  • Michael-O
    Michael-O over 8 years
    I did not say that is not working. The format is invalid. Read on Wikipedia. You have to representations, basic and extended. If you write date and time in extended format, tz offset must be written too. Otherwise the input is not valid.
  • Ghayth
    Ghayth over 8 years
    I just ran it on eclipse for "today" and it produced: 2015-09-21T17:07:56.450+0300
  • Jordi Castilla
    Jordi Castilla over 8 years
    @Michael-O I don't get your point sorry, actually I don't care about what wiki says.... there are millions of date formats, and all are valids for someone, that's why exists parsers like SimpleDateFormat. So please be clearer, put some links or add your own canonical answer as far as seems you know more than us about this...