4

对于一个生活在Molossia 共和国的人来说,Java 中的以下代码:

String TimeZoneId=TimeZone.getDefault().getID();
System.out.println(TimeZoneId);

将打印

美国/里贾纳

我试过TimeZoneInfo.GetSystemTimeZones()了,但返回值似乎不是 tzdb id。C#中是否有等效的方法?

4

3 回答 3

4

.Net 并不自然地支持 TZDB,因为微软有它自己的时区数据库。有关详细信息,请参阅TimeZone 标记 wiki

如果要使用 TZDB 区域,请使用NodaTime库。你问的确切翻译是:

using NodaTime;

...

var timeZoneId = DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
Console.WriteLine(timeZoneId);
于 2013-04-13T17:05:22.390 回答
1

Typically this can be done using the TimeZone class. This is the closest that you will get with the native libraries from my understanding. Why do you need that specific result?

Take a look at Microsoft Time Zones and the Conversion Table* for the Windows Equivalent as Windows does not fully support the TZID, take a look at the stack overflow question below for more information. I hope this is more useful as I misread your previous question!!! - sorry!

//Get A reference to our timezone object
TimeZone t = TimeZone.CurrentTimeZone;

//Print out the display name
Console.WriteLine(t.StandardName);

MSDN REFERENCE

http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html

Please refer this previous stackoverflow question .NET TimeZoneInfo from Olson time zone

于 2013-04-13T11:47:55.187 回答
1

类似的值由

Calendar.GetTimeZone

欲了解更多信息:https ://docs.microsoft.com/en-us/uwp/api/windows.globalization.calendar.gettimezone?view=winrt-20348

于 2021-06-12T17:46:21.757 回答