How to convert a time zone information in C# to a

2019-05-02 10:25发布

I have a system which consists of a C# back end and a Java front end. The C# back end communicates with other systems and some mobile devices.

On the C# side, my task is among others to recognize the time zone of the timestamps coming from the mobile devices and create the corresponding TimeZoneInfo object. This is working without any problems.

On the Java side, I have to display the time zone information together with the data sent by a mobile device. I'm using the TimeZone class to store the time zone information within my domain objects.

Now the question is how can I create a Java's TimeZone object which corresponds with a C#'s TimeZoneInfo object? AFAIK the time zones do not have any unique IDs. Further on, the names are also different (e.g. in C#: "Central Europe Standard Time", in Java "Central Europe Time"). Not even the number of the time zones in C# and in Java is equal!

标签: c# java timezone
2条回答
Evening l夕情丶
2楼-- · 2019-05-02 11:15

As paulsm4 says, you can map Windows names to TZDB (aka Olson, aka tz, aka zoneinfo) names - although the mapping changes periodically, and you should really look at Unicode CLDR for more details. You should be aware that the format of the mapping table has changed over time, and that some Windows IDs map to multiple TZDB IDs. (Some are missing entirely.)

As an alternative, you could consider abandoning Windows time zone IDs entirely, and use TZDB throughout the stack, if you use Noda Time, a .NET date/time API I've been working on for a while now. You'll also find your Java date/time work easier if you use Joda Time, which is the library I based the "engine" of Noda Time on. It's much nicer than using Date and Calendar in Java.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-02 11:21

You know that "time" is independent of "time zone", so I won't belabor that point :)

"TimeZone" is really more a function of your OS than a programming language.

Android "Time Zone IDs" correspond to the standard, IANA "Olson database":

The other half of the equation is mapping .Net timezones to the "standard". This link should help:

查看更多
登录 后发表回答