简体   繁体   中英

Why should I not use abbreviations in Java TimeZone

Core problem: "GMT" TimeZone returns things as desired. "etc/GMT" returns with an additional "+0:00" which is not desired. "etc/Greenwich" seems to confuse people that it is referring to a specific location, this is not desired.

So why is it bad practice to use "GMT"?

Original Post

Per the Java Doc

Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.

I am trying to use this so I went to "etc/GMT" but when I use the the output looks like...

But I don't want the redundant +0.00. If I use the abbreviation it doesn't show the +0.00, neither does 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). The other option of trimming the +0.00 also seems to not be a good solution.

Why are abbreviations no longer supported, and what is the proper TimeZone to get if I don't want the +0.00

Using Java 1.7.51 (groovy code as well)

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)

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

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)

I see...

19-Aug-2014 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.

In your case, you should use one of the timezones that are equivalent to GMT :

Etc/GMT
Etc/UTC
Etc/Universal

Note that those are not abbreviations. Those are the full names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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