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,813
  • 5
  • 66
  • 90
user93796
  • 18,749
  • 31
  • 94
  • 150
  • 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
  • 588,226
  • 146
  • 1,060
  • 1,140
2

You should defintely take a look at Joda-Time:

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

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
  • 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
  • 37,247
  • 13
  • 80
  • 152