1

Possible Duplicate:
Converting ISO8601-compliant String to java.util.Date

I have issues when changing the timestamp to ISO 8601 in JS as it errors at 'topicDate' in IE and Firefox, but it works in Chrome. So i want to change the timestamp to ISO 8601 in the server side and send that via json instead. Can anyone help me how to convert the below time stamp to ISO 8601 format in Java using standard classes? Any other suggesting about this approach is also welcomed.

Time sent via json

 "topic_lstUpdate" : "2012-09-07 19:39:56.439",

JS script

var topicDate = new Date(args.topic_lstUpdate);
            var topicDateISO = topicDate.toISOString();
            var topicDateTimeago=jQuery.timeago(topicDate);
Community
  • 1
  • 1
user1595858
  • 3,700
  • 15
  • 66
  • 109
  • Older duplicate: [Given a DateTime object, how do I get a ISO 8601 date in string format?](http://stackoverflow.com/q/114983/642706). – Basil Bourque Jun 02 '15 at 01:41

2 Answers2

4

To format within Java on the server-side:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String my8601formattedDate = df.format(new Date());

It is recommended that you include the T delimiter - but if you're certain your requirements on both ends permit excluding it, you are permitted to omit it. See http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations for details.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • Your should use UTC timezone as GMT does observe daylight savings. – Steve Kuo Sep 08 '12 at 00:28
  • 1
    @SteveKuo - Agreed that "UTC" is probably a better option - but "GMT" will work identically. `sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]` – ziesemer Sep 08 '12 at 00:32
  • You are correct, GMT=UTC. GMT does not have daylight savings. In the summer the UK uses British Summer Time. – Steve Kuo Sep 08 '12 at 00:36
  • 2
    @user1595858 - A proper timestamp must include a timezone - otherwise, it is ambiguous. That said, if your requirements are in agreement that an assumed timezone is always in effect, you could potentially omit this. Note that the 2nd line of my code actually adjusts the displayed time (generally, the hour #) to be correct in UTC/GMT - and this is _indicated_ in the returned String by the `Z` - which you could also omit. – ziesemer Sep 08 '12 at 01:46
0

It's almost there already. You can use this JavaScript to convert it:

topicDate.replace(" ", "T");
Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62