English 中文(简体)
• 如何处理“地方时间与统一技术转换”在NET/java中不同执行
原标题:How to handle that "Local Time to UTC Conversion" differently implemented in .NET / java

.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.

因此,这似乎行不通。 日期 在美洲金枪鱼委交换时间并提供服务。 如果在将当地时间改为万国邮联时,网络的时代和 Java的日历将完全考虑到同样的规则,那将是罚款。

<>光> 自1979年以来,瑞士拥有德国航空航天局。 Java知道,以前不存在数字数据系统。 NET自此就一直存在。 因此,在1979年之前的夏天,日期从......改为: 从网络到UTC,从Java回到当地时间(用于介绍),这个日期在同一个时间区损失了一个小时。

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...

感谢你们的时间。

最佳回答

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.

最好的序列器是 Newtonsoft s Json.NET(NuGet page ,成为事实上的标准。 NET开发商。 它们有很短的时间序列化/代号化选择,可以简单地使用:。 详情请在日期上填写博客。 我倾向于使用ISO 8601,这是其在Json.NET诉4.5中的缺省格式。

我曾利用Json与许多不同产品用多种语言和Json进行沟通的企业申请。 NET使这一产品间通信变得微不足道。

www.un.org/spanish/ecosoc 关于与瑞士交往的最新信息

由于你重新规定一个通用的中欧标准时间,NET并不了解瑞士的具体日期规则。 为了与瑞士打交道,你再次需要提供瑞士规则网。 看看Wikipedia,它始于1981年,因此形成了一个惯例<代码>。 时间ZoneInfo将研究如下内容:

// UTC Time
var date1 = new DateTime(1969, 4, 20, 2, 20,00, DateTimeKind.Utc);
Console.WriteLine("Date 1: " + date1.ToString() + " - " + date1.IsDaylightSavingTime());

// CEST
var timezone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var date2 = TimeZoneInfo.ConvertTimeFromUtc(date1, timezone);
Console.WriteLine("Date 2: " + date2.ToString() + " - " + date2.IsDaylightSavingTime() + " " + timezone.IsAmbiguousTime(date2));

// Switzerland
var cesAdjRule = timezone.GetAdjustmentRules().Single();
var switzerlandStartTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(
        cesAdjRule.DaylightTransitionStart.TimeOfDay,
        cesAdjRule.DaylightTransitionStart.Month, cesAdjRule.DaylightTransitionStart.Week,
        cesAdjRule.DaylightTransitionStart.DayOfWeek
);
var switzerlandAdjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(
    new DateTime(1981, 1, 1),
    DateTime.MaxValue.Date,
    cesAdjRule.DaylightDelta,
    switzerlandStartTransition,
    cesAdjRule.DaylightTransitionEnd
);
TimeZoneInfo.AdjustmentRule[] adjustments = {switzerlandAdjustmentRule};

var switzerlandTimeZone = TimeZoneInfo.CreateCustomTimeZone("Switzerland",
                                                            timezone.BaseUtcOffset,
                                                            "Switzerland",
                                                            "Switzerland",
                                                            "Switzerland",
                                                            adjustments, false);

var date3 = TimeZoneInfo.ConvertTimeFromUtc(date, timezone, switzerlandTimeZone);
Console.WriteLine("Date 3: " + date3.ToString() + " - " + date3.IsDaylightSavingTime() + " " + timezone.IsAmbiguousTime(date3));

这样做的结果(至少在我的机器上):

Date 1: 4/20/1969 2:20:00 AM - False
Date 2: 4/20/1969 4:20:00 AM - True - False
Date 3: 4/20/1969 3:20:00 AM - True - False

如你所知,IsDaylightSavingstime is real on the Datetime,但timeZoneInfo正确地改变了我们的时间。 其他几个组合也看好。 您也可以在认为你与瑞士有<代码>的无害环境技术之间转换。 TimeZoneInfo.Coverttime()。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签