Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

timezone

c#

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!

like image 781
Tomas Walek Avatar asked Aug 23 '12 07:08

Tomas Walek


People also ask

How does C handle different time zones?

You can use gmtime() and the tm structure to directly set this, provided you know the offsets. If you know your local time and UTC, you know your local offset. Provided you also know the target offset, it's just a matter of setting tm_hour appropriate (and potentially flipping the day, too, if you go <0 or >23).

How do you change from one time zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

What is the time zone in C?

Charlie Time Zone is also commonly used at sea between longitudes 37.5° East and 52.5° East. The letter C may be used as a suffix to denote a time being in the Charlie Time Zone, such as 08:00C or 0800C. This is spoken as "zero eight hundred Charlie".

How do you convert UTC time to local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.


2 Answers

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":

  • http://developer.android.com/reference/java/util/TimeZone.html#getTimeZone%28java.lang.String%29

  • http://en.wikipedia.org/wiki/Tz_database

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

  • .NET TimeZoneInfo from Olson time zone
like image 134
paulsm4 Avatar answered Sep 26 '22 10:09

paulsm4


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.

like image 21
Jon Skeet Avatar answered Sep 26 '22 10:09

Jon Skeet