2

I am trying to convert date in String format to Date data type .

I am using SimpleDateFormatter.

But my date in String is in format 2012-12-24T16:45:00.000+05:30

How can I use the simple sdf to convert?

Is this possible in Java?

As My code is in Java.

gprathour
  • 14,313
  • 5
  • 60
  • 89
user93796
  • 17,671
  • 30
  • 88
  • 140
  • possible duplicate of [Converting ISO8601-compliant String to java.util.Date](http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date) – jarnbjo Jan 24 '12 at 13:01
  • You can't use SimpleDateFormat for ISO8601 encoded time stamps. You can read my response to the linked question for a different solution. – jarnbjo Jan 24 '12 at 13:02

3 Answers3

4

Use a SimpleDateFormat (or joda-time DateTimeFormatter):

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = df.parse(str);

The timezone part won't work with Java6. It's XXX in Java7, and ZZ in joda-time.

Bozho
  • 572,413
  • 138
  • 1,043
  • 1,132
2

You should defintely take a look at Joda-Time:

http://joda-time.sourceforge.net/

Alexis Dufrenoy
  • 11,484
  • 11
  • 80
  • 123
  • Joda time really rules, since your format is iso i believe you can use `DateTime dt = new DateTime("2012-12-24T16:45:00.000+05:30");` (from the top of my head, i didn't check, just remembered). – clankill3r Jan 25 '12 at 21:45
0

Where did you get this String from? Xml? Then use the following method to parse the String:

http://docs.oracle.com/javase/7/docs/api/javax/xml/datatype/DatatypeFactory.html#newXMLGregorianCalendar%28java.lang.String%29

Also consider using JAXB when working with XML.

Puce
  • 36,099
  • 12
  • 75
  • 145