.NET Web Service
I have a .NET web service which returns json formatted DateTime’s.
The json representation of a DateTime specified by Microsoft is for example:
/Date(1302847200000+0200)/
This is according to MSDN – Stand-Alone JSON Serialization the millisecond offset since the first of January 1970 in UTC before the +/- sign and after the sign the time zone offset of the local time to GMT. So for this serialization the DateTime value must be converted from the web service’s local time to UTC.
Android (java) Client
In my Android application, which receives the web service results, I parse json using Gson. Because the .NET DateTime json serialization is not a standard (according to Microsoft there is no such standard for DateTime) gson is not able to parse such formatted dates. I wrote a Date TypeAdapter which does the serialization and deserialization on the client side.
So this seems to work fine. DateTime exchange from and to service in UTC. It would be fine, if .NET’s DateTime and Java’s Calendar would regard totally the same rules when it comes to conversion of local to UTC time.
An example: Switzerland has DST (Daylight Saving Time) since 1979. Java knows that DST did not exist before. .NET assumes it exists since ever. So when a date in summer before 1979 is converted from .NET to UTC and from Java back to Local time (for presentation), then this date loses an hour in the same time zone.
Question
How to face that problem? Is there any mistake I am doing in the described data exchange.
I thought about many different solutions. The only way which i can imagine to work properly is to replace the .NET json DateTime Serialization/Deserialization. This of course is not a favoured one…
Thanks for your time.
Don’t use the standard .NET Json serializer it will cause you too many headaches than you need if you are working between Java and .NET. As you found out, Microsoft uses a format that most people don’t use.
The best serializer is Newtonsoft’s Json.NET (NuGet page) it’s become a de facto standard for .NET developers. They have a great DateTime serialization/deserialization options that are simple to use. For more information check out their blog post on DateTime. I tend to use ISO 8601, which is their default format in Json.NET v4.5.
I used to work on an enterprise application that worked communicated via Json with many different products create in a range of languages and Json.NET made that inter-product communication trivial.
Update on dealing with Switzerland’s DateTime
Since you’re specifying a generic Central European Standard Time, .NET doesn’t know what Switzerland’s date time rules are specifically. To deal with Switzerland you’re going to need to give .NET the Switzerland rules. Looking at Wikipedia it started in 1981, so creating a custom
TimeZoneInfowould look something like the following:The output of that would look like this (at least on my machine):
As you can see, IsDaylightSavingsTime is True on the
DateTime, butTimeZoneInfocorrectly converted our time. Trying a few other combinations looks good too. You can also convert between the CEST that it thinks you have and Switzerland withTimeZoneInfo.CovertTime().