3
String stringDate="2014-03-20T17:59:03+07:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date startDate = df.parse(stringDate);

But I got an error that java.text.ParseException: Unparseable date: "2014-03-20T17:59:03+07:00"

  • 5
    You realize that the format you provide to the simple date formatter should match the format of the date you're attempting to parse? – Evan Knowles May 05 '14 at 06:13
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Oleg Estekhin May 05 '14 at 06:16

4 Answers4

2

Format Wrong:

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

here

Edit :

String stringDate="2014-03-20T17:59:03+07:00";//2014-03-20T17:59:03+07:00 2014-03-20T17:59:03-07:00 
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date startDate = sdf.parse(stringDate);
            System.out.println(startDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Sam
  • 1,124
  • 1
  • 8
  • 12
  • [java.text.ParseException] **: Unparseable date: "2014-03-20T10:59:03Z"** –  May 05 '14 at 06:53
0

Take a look at the format you are asking for df to parse, and the one you are giving it. stringDate is formatted as "yyyy-mm-ddThh:mm:33+ss:ss", but df expects "dd/mm/yyyy HH:mm:ss". To fix this either change the format of your input or the format of df.

donutmonger
  • 154
  • 1
  • 5
0

The format string for parsing you're looking for is yyyy-MM-dd'T'HH:mm:ssZ, however Z expects a format of +0700 for the time zone, instead of +07:00, so you need to do some string replacement first, to get rid of the colon.

String stringDate = "2014-03-20T17:59:03+07:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date startDate = df.parse(stringDate.replaceAll("([+-]\\d{2}):(\\d{2})$", "$1$2"));
System.out.println(startDate); // output "Thu Mar 20 11:59:03 CET 2014" in my time zone
qqilihq
  • 10,794
  • 7
  • 48
  • 89
0

The Joda-Time library accepts these ISO 8601 strings directly, parsed with a built-in formatter.

DateTime dateTime = new DateTime( "2014-03-20T17:59:03+07:00" );

If you really need a java.util.Date object (best to avoid j.u.Date when you can), convert.

java.util.Date date = dateTime.toDate();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154