What Timezone Abbreviation does Java use for your location?

8 ビュー (過去 30 日間)
James Tursa
James Tursa 2011 年 2 月 21 日
回答済み: Ellie 2022 年 7 月 8 日
I am writing timezone sensitive versions of several MATLAB date related functions (datenum, datestr, datevec, etc) for eventual posting to the FEX and have been using the abbreviations at the following link as my guide:
Naturally, I would like my code (using the above) to be consistent with other s/w on the computer as far as timezone is concerned. Soooo ... I would like to invite the community to post the results of the following MATLAB code to this thread to confirm or refute that list. Specifically, I am looking to see that the timezone number and abbreviation reported by Java matches the abbreviation and number in the above list.
java.util.Date() % The date string display
-ans.getTimezoneOffset()/60 % the timezone offset from UTC
If you don't see your particular timezone already in the answers or you see a discrepancy with another answer already posted please post your results. Thanks! (I would do this myself ... but ... alas, I only live in one timezone)

採用された回答

Matt Fig
Matt Fig 2011 年 2 月 22 日
Everything looks kosher here:
>> java.util.Date() % The date string display
-ans.getTimezoneOffset()/60 % the timezone offset from UTC
ans =
Mon Feb 21 18:08:52 MST 2011
ans =
-7

その他の回答 (4 件)

James Tursa
James Tursa 2011 年 2 月 22 日
>> java.util.Date() % The date string display
ans =
Mon Feb 21 15:59:56 PST 2011
>> -ans.getTimezoneOffset()/60 % the timezone offset from UTC
ans =
-8

Paulo Silva
Paulo Silva 2011 年 2 月 22 日
Tue Feb 22 01:16:41 GMT 2011
0

Walter Roberson
Walter Roberson 2011 年 2 月 22 日
Currently CST (-6), summer time is CDT (-5)

Ellie
Ellie 2022 年 7 月 8 日
Hey, My name is Ellie White and i have completed my java training with certification from SynergisticIT. It sounds like a simple question, but the answer is complicated. The time zone abbreviations supported by Java (whether we are talking the modern DateTimeFormatter or the troublesome and outdated SimpleDateFormat) are not defined by Java itself but by the locale data that Java uses. Complications include
  • Java can be configured to take locale data from different sources. This means that setting the system property java.locale.providers when you run your program will change the set of abbreviations supported.
  • The default locale providers were changed from Java 8 to Java 9. So running your program on a different Java version will give you a different set of supported time zone abbreviations.
  • CLDR, the locale data that are the default from Java 9, come in versions. So every new Java version may come with a different set of supported abbreviations.
  • Time zone abbreviations come in languages. So a formatter with French locale will support other abbreviations than a formatter with German locale, for example.
  • Time zone abbreviations are ambiguous. So even if PST is a supported abbreviation, you don’t know whether it parses into Pitcairn Standard Time, Pacific Standard Time or Philippine Standard Time.
One way to get an idea: Run a program formatting dates at different times of year into a time zone abbreviation. Since many time zones use summer time (DST) and use a different abbreviation during summer time, you will want to try to hit a date in the summer and a date in the standard time of year for those zones.
System.out.println("java.locale.providers: " + System.getProperty("java.locale.providers"));
DateTimeFormatter zoneAbbreviationFormatter = DateTimeFormatter.ofPattern("zzz", Locale.FRENCH);
Instant northernSummer = Instant.parse("2019-07-01T00:00:00Z");
Instant southernSummer = Instant.parse("2020-01-01T00:00:00Z");
Set<String> supportedAbbreviations = new TreeSet<>();
for (String zid : ZoneId.getAvailableZoneIds()) {
ZoneId zone = ZoneId.of(zid);
supportedAbbreviations.add(northernSummer.atZone(zone).format(zoneAbbreviationFormatter));
supportedAbbreviations.add(southernSummer.atZone(zone).format(zoneAbbreviationFormatter));
}
System.out.println("" + supportedAbbreviations.size() + " abbreviations");
System.out.println(supportedAbbreviations);

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by