Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :(

like image 938
praveen_mohan Avatar asked Sep 21 '15 11:09

praveen_mohan


People also ask

What is difference between MM and MM in date format?

The values should be 'mm' for minute and 'MM' for month.

What is TZ format date?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).


2 Answers

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
like image 74
Jordi Castilla Avatar answered Oct 16 '22 16:10

Jordi Castilla


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)

like image 41
Ghayth Avatar answered Oct 16 '22 17:10

Ghayth