tribal tribal - 3 months ago 10
MySQL Question

Jersey has written my mysql timestamp as 2011-09-28T21:48:25Z how do I format it in Java?

I have a mysql backend that has a timestamp field that is auto set as currenttimestamp like so (

date_time
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP). After reading the value as Date, Jersey sends out an xml where the timestamp shows as

<timestamp>2011-09-28T21:48:25Z</timestamp>


Please don't make too much of the backstory: I can't change what I get from Jersey. Now my question is this: how do I parse the 2011-09-28T21:48:25Z from xml as a date that Java understands?

Thanks.

Answer

First,

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") ;
df.setTimeZone(TimeZone.getTimeZone("Zulu")) ;
Date dd = df.parse("2011-09-28T21:48:25Z") ;

...then, optionally,

Timestamp ts = new Timestamp(dd.getTime()) ;