converting String to date in java (complex string )

2.4k Views Asked by At

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.

3

There are 3 best solutions below

2
Bozho On

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.

1
Alexis Dufrenoy On

You should defintely take a look at Joda-Time:

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

0
Puce On

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.