java TimeZone.setTimeZone("est") 与 TimeZone.setTimeZone("EST") 不同吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13230937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:59:01  来源:igfitidea点击:

Is TimeZone.setTimeZone("est") different from TimeZone.setTimeZone("EST")

javacalendar

提问by Abubakkar

When I write this code:

当我写这段代码时:

   Calendar cal = Calendar.getInstance();
   cal.setTimeZone(TimeZone.getTimeZone("EST"));
   System.out.println(cal.getTimeZone().getDisplayName());

The output is

输出是

   Eastern Standard Time

But when I write this code:

但是当我写这段代码时:

   Calendar cal = Calendar.getInstance();
   cal.setTimeZone(TimeZone.getTimeZone("est"));
   System.out.println(cal.getTimeZone().getDisplayName());

The output I get is:

我得到的输出是:

   GMT-05:00

Is there any difference in giving arguments like "EST" and "est" when setting TimeZone.setTimeZone(String str)(Is strto be passed when calling considered CASE SENSITIVE)?

在设置时给出“EST”和“est”之类的参数有什么区别TimeZone.setTimeZone(String str)str在调用考虑大小写时要传递)?

The API doesn't mention anything about it:

API 没有提及任何关于它的内容:

getTimeZone

public static TimeZone getTimeZone(String ID)

Gets the TimeZone for the given ID.

Parameters:  
ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00".   
Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.

Returns:
the specified TimeZone, or the GMT zone if the given ID cannot be understood.

NOTE: I tried with ISTand iststrings. For ISTstring it gives Indian Standard Timeand for istit gives Greenwich Mean Time

注意:我尝试使用ISTist字符串。对于IST它给出的字符串Indian Standard Timeist它给出的Greenwich Mean Time

采纳答案by Mathieu Le Tiec

In short, yes it is case sensitive.

简而言之,是的,它区分大小写。

Following your examples istgives you GMTbecause a timezone with such an ID can't be find, thus giving you default result

按照您的示例ist为您提供,GMT因为无法找到具有此类 ID 的时区,从而为您提供默认结果

it works with est(GMT-05:00is EASTERN STANDARD TIME), surely because both IDs are known, but I wouldn't count on it (not too sure it would still be there if change platform).

它适用于est( GMT-05:00is EASTERN STANDARD TIME),当然是因为两个 ID 都是已知的,但我不会指望它(不太确定如果更换平台它仍然存在)。

Furthermore, as told by the API you shouldn't use those abbreviated IDs but directly full name or custom ID.

此外,正如 API 所告知的,您不应使用那些缩写的 ID,而应直接使用全名或自定义 ID。

You can have a list of your platform's available IDs using TimeZone.getAvailableIDs(), then you can pick the right one.

您可以使用 获得平台可用 ID 的列表TimeZone.getAvailableIDs(),然后您可以选择正确的ID 。

I would myself consider using GMT-5:00format, which is in my opinion more readable and less error prone.

我自己会考虑使用GMT-5:00格式,在我看来,它更具可读性且不易出错。

回答by Ekkelenkamp

The implementation of getTimeZone(String id) actually changed from JDK 7 to 8.

getTimeZone(String id) 的实现实际上从 JDK 7 变成了 8。

In JDK 7 "est" is actually returning a timeZone with id "est". Running the following test case on java 7 will succeed (and fail on java 8):

在 JDK 7 中,“est”实际上是返回一个 ID 为“est”的时区。在 java 7 上运行以下测试用例将成功(在 java 8 上失败):

@Test
public void estTimeZoneJava7() {
    TimeZone timeZone = TimeZone.getTimeZone("est");
    assertEquals("est", timeZone.getID()) ;
}

With Java 8 the timezone "est" is actually handled as a unknown timezone and will in fact return the GMT timezone with id "GMT". The following test case will succeed on Java 8 (and fail on java 7).

在 Java 8 中,时区“est”实际上是作为未知时区处理的,实际上会返回 ID 为“GMT”的 GMT 时区。以下测试用例将在 Java 8 上成功(在 java 7 上失败)。

@Test
public void estTimeZoneJava8() {
    TimeZone timeZone = TimeZone.getTimeZone("est");
    assertEquals("GMT", timeZone.getID());
}