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); 
1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Your should use UTC timezone as GMT does observe daylight savings.
@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]
You are correct, GMT=UTC. GMT does not have daylight savings. In the summer the UK uses British Summer Time.
@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.
0

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

topicDate.replace(" ", "T"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.