I tried to use SimpleDateFormat to do the work, but I don't know how to handle the T in the string "2008-08-01T15:47:00.557", can anyone help me with this?
5 Answers
You need to use the format "yyyy-MM-dd'T'hh:mm:ss.SSS".
In an additional note, if you are trying to handle xml dates check out this question: Convert Java Date into XML Date Format (and vice versa)

- 1
- 1

- 6,284
- 26
- 25
I'm not very very sure. But if I remember good, you have to surround the T by single quotes in your format.
String yourFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";

- 67,591
- 47
- 198
- 287
-
1@faz: That's great. I see you are new here. When an answer helped you out, you can accept that answer by clicking the tick at the left of the answer. This way the question is marked as closed and the answerer gets rewarded for the given answer. – Martijn Courteaux Nov 04 '11 at 23:59
ISO 8601
Your string’s format happens to comply with the ISO 8601 standard.
java.time
Java 8 and later includes the java.time framework to supplant the old date-time classes used in the Question and in other Answers.
The new classes use the ISO 8601 standard by default when parsing/generating strings. So no need to specify a coded format pattern.
Time Zone
Your input string lacks any time zone or offset-from-UTC. So you must specify the time zone for which this string has meaning. If you do not specify, the parsing automatically applies your JVM’s current default time zone. Not good as that default may not be the zone intended for your string. Also, the JVM’s default can change at any moment, even during runtime.
If UTC
If your string was meant for UTC as the time zone, simply append a Z
(short for “Zulu” which means UTC). Then parse as an Instant
, a moment on the timeline in UTC.
String input = "2008-08-01T15:47:00.557";
Instant instant = Instant.parse ( input + "Z" );
Dump to console.
System.out.println ( "instant: " + instant );
instant: 2008-08-01T15:47:00.557Z
If Time Zone
If your string was intended for some other time zone, we need to specify. Use a proper time zone name (never the 3-4 letter codes seen in the press). Here we arbitrarily choose the Montréal time zone.
For the formatting pattern, we use one of the predefined formats for ISO 8601: DateTimeFormatter.ISO_LOCAL_DATE_TIME
(the 'LOCAL' means no time zone or offset embedded within the input string).
String input = "2008-08-01T15:47:00.557";
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
formatter = formatter.withZone ( zoneId );
ZonedDateTime zdt = ZonedDateTime.parse ( input , formatter );
Dump to console. We also extract an Instant
from the ZonedDateTime
so you can see the same moment in UTC. Usually best to work in UTC in your business logic; only apply a time zone for output to the user.
System.out.println ( "input: " + input + " | zdt: " + zdt + " | instant of zdt: " + zdt.toInstant () );
input: 2008-08-01T15:47:00.557 | zdt: 2008-08-01T15:47:00.557-04:00[America/Montreal] | instant of zdt: 2008-08-01T19:47:00.557Z

- 303,325
- 100
- 852
- 1,154
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Almost this exact example is given in the API, check it out :-) http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

- 601
- 3
- 9
since your example was with 24H format and not AM/PM one
you should use HH (capital) instead of hh
like this
String EXT_JS_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

- 36,833
- 10
- 119
- 200