0

I am trying to convert this date 2013-03-04T12:08:52.74+08:00

formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = formatter.parse(dateresult);

But it ends with an exception

03-07 19:17:05.493: W/System.err(2006): java.text.ParseException: Unparseable date: "2013-03-04T12:08:52.74+08:00" (at offset 19)

I tried this. But same exception occured. Does anyone know of another (possibly better) way to accomplish this ?

Community
  • 1
  • 1
ASP
  • 81
  • 1
  • 1
  • 4
  • 4
    i think you have millisecond part in your string `.74` but have not mentioned it in your format string – Abubakkar Mar 07 '13 at 13:58
  • 1
    This is the ISO 8601 format. This SO question may helps you: http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date – Miguel Prz Mar 07 '13 at 14:02
  • @Abu oh.. I mssd it.. bt it doesn't make any progress. And now I've used Joda-Time and worked. Thanx for ur reply :) – ASP Mar 08 '13 at 05:49
  • @MiguelPrz thanx bro.. that link help me lot :) – ASP Mar 08 '13 at 05:50

4 Answers4

1

You can use joda-time. It supports ISO8601 standard.

joda-time manual

Sample:

DateTime dt = new DateTime("2013-03-04T12:08:52.74+08:00");
System.out.println(dt.toString());
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
System.out.println(
        fmt.parseDateTime("2013-03-04T12:08:52.74+08:00"));
longhua
  • 4,142
  • 21
  • 28
0

Use:

formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");

To include milliseconds (.74)

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

In addition to you missing the milliseconds as baraky pointed out, you'll also run into the ISO-8601 time zone issue when parsing dates with Java. It was addressed in this question.

Community
  • 1
  • 1
wonderb0lt
  • 2,035
  • 1
  • 23
  • 37
0

Also, in addition to @baraky's answer, your input date's time zone format does not conform to your format string's specification. Your format string has 'Z' (the literal letter Z). It seems that your input string has the format X. So you might want to use:

formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSX");

frIT
  • 3,213
  • 1
  • 18
  • 22