开发者

GWT JsDate serialize into JSON

I am having issues serializing correct JSON for date fields in gwt.

In gwt, I have an overlay class with a JsDate field called 'dtTemp'. Outside of the object, I set the value like this: myOverlayObj.setDtTemp(JsDate.create(1999, 10, 1));

I have a "toJSON" method in the overlay class which converts the object to JSON which can be passed back to my ASP.NET web service. It looks like this:

public final String toJSON(){ return new JSONObject(this).toString();

}

Unfortunately, it seems that the JSON being generated has bad dates. It creates this JSON for an object where dtTemp is SUPPOSED TO BE 10/1/1999: {"intTemp":7007, "strTemp":"hello", "dtTemp":941439600000}

when it should create this (I created this parallel object in .NET using a JSON serializer): {"intTemp":7007, "strTemp":"hello", "dtTemp":new Date(938761200000)}

my question is, how can I serialize valid dates in gwt? Not only is it missing 'new Date' but it's a totally different number! (when evaluate the Datetime in .NET using JSON.NET the date that 'toJSON' generated ends up to be s开发者_JAVA技巧omething like 11/1/1999 at 7AM)

Any help would be appreciated!

Thanks!


JSON cannot represent dates: http://www.json.org

new Date is JavaScript, it's not JSON.

As for the difference in the unix timestamp, there are two issues:

  1. first, JsDate.create(1999, 10, 1) really represents 11/1/1999, and not 10/1/1999: months in the API are 0-based, so 10 is November.
  2. The "7AM" is due to the timezone: 938761200000 is 10/1/1999 7AM UTC, and 941439600000 is 11/1/1999 7AM UTC. You're probably at UTC+7, so your 10/1/1999 00:00 UTC+7 really is 10/1/1999 07:00 UTC: when you create a date using Jsdate.create, it's relatie to your timezone (so it's 00:00 UTC+7 in your case), and your server then shows the date in UTC (which gives 7AM). You should use JsDate.UTC(1999, 9, 1, 0, 0, 0, 0) to create your instance.

I highly recommend reading: http://unix4lyfe.org/time/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜