简体   繁体   English

为什么我不应该在Java TimeZone中使用缩写

[英]Why should I not use abbreviations in Java TimeZone

Core problem: "GMT" TimeZone returns things as desired. 核心问题:“ GMT” TimeZone返回所需的内容。 "etc/GMT" returns with an additional "+0:00" which is not desired. “ etc / GMT”返回不需要的附加“ +0:00”。 "etc/Greenwich" seems to confuse people that it is referring to a specific location, this is not desired. “ etc / Greenwich”似乎使人们误以为它指的是特定位置,这是不希望的。

So why is it bad practice to use "GMT"? 那么,为什么不习惯使用“ GMT”呢?

Original Post 原始帖子

Per the Java Doc 根据Java文档

Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used. 请注意,缩写的支持仅出于JDK 1.1.x兼容性的考虑,应使用全名。

I am trying to use this so I went to "etc/GMT" but when I use the the output looks like... 我试图使用它,所以我去了“ etc / GMT”,但是当我使用输出时,看起来像...

But I don't want the redundant +0.00. 但我不要多余的+0.00。 If I use the abbreviation it doesn't show the +0.00, neither does etc/Greenwich. 如果使用缩写,则不会显示+0.00,而etc / Greenwich也不会。 Both of these options seem bad because one is explicitly wrong given the doc (GMT) and one is a little too specific to a location (etc/greenwich). 这两个选项似乎都是不好的,因为给定文档(GMT)时,一个明显是错误的,而另一个对于位置(etc / greenwich)太具体了。 The other option of trimming the +0.00 also seems to not be a good solution. 修整+0.00的另一种选择似乎也不是一个好的解决方案。

Why are abbreviations no longer supported, and what is the proper TimeZone to get if I don't want the +0.00 为什么不再支持缩写?如果我不希望+0.00,可以使用什么正确的TimeZone?

Using Java 1.7.51 (groovy code as well) 使用Java 1.7.51(也包括Groovy代码)

Adding Code 添加代码

private String formatET(Date etDate){
    StringBuilder sb = new StringBuilder();
    SimpleDateFormat etFormat = new SimpleDateFormat("dd-MMM-yyy kk:mm zzz")
    TimeZone etTimeZone = TimeZone.getTimeZone("America/New_York")
    etFormat.setTimeZone(etTimeZone)
    sb.append(etFormat.format(etDate))

    TimeZone gmtTimeZone = TimeZone.getTimeZone("etc/GMT")
    SimpleDateFormat gmtFormat = new SimpleDateFormat("kk:mm zzz")
    gmtFormat.setTimeZone(gmtTimeZone)
    sb.append(" (${gmtFormat.format(etDate)})")
    sb.toString();
}

This returns the following... 这将返回以下内容...

11-Aug-14 11:48 EDT (15:48 GMT+00:00) 2014年8月11日11:48 EDT(15:48 GMT + 00:00)

I tried to add it to this... 我试图将其添加到此...

import java.text.SimpleDateFormat;
import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        Date d = new Date();
        SimpleDateFormat etFormat = new SimpleDateFormat("dd-MMM-yyy kk:mm zzz");
        TimeZone etTimeZone = TimeZone.getTimeZone("etc/GMT");
        etFormat.setTimeZone(etTimeZone);
        System.out.println(etFormat.format(d));
     }
}

And putting that here... 放到这里...

http://www.compileonline.com/compile_java_online.php http://www.compileonline.com/compile_java_online.php

And it works like I would want. 它就像我想要的那样工作。 When I go back to run locally using 当我回到本地使用

java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode) Java版本“ 1.7.0_51” Java™SE运行时环境(内部版本1.7.0_51-b13)Java HotSpot(TM)64位服务器VM(内部版本24.51-b03,混合模式)

I see... 我懂了...

19-Aug-2014 15:53 GMT+00:00 2014年8月19日15:53 GMT + 00:00

Abbreviations are not recommended because they are not unique. 不建议使用缩写,因为它们不是唯一的。 For example, EST stands for both Eastern Standard Time in the US and in the east of Australia. 例如,EST表示美国和澳大利亚东部的东部标准时间。

In your case, you should use one of the timezones that are equivalent to GMT : 在您的情况下,您应该使用与GMT等效的时区之一:

Etc/GMT ETC / GMT
Etc/UTC ETC / UTC
Etc/Universal ETC /通用

Note that those are not abbreviations. 请注意,这些不是缩写。 Those are the full names. 这些是全名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM