0

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

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • Also fun fact yyy is 2 digit year in JVM6 and 4 in JVM7 – Jackie Aug 19 '14 at 15:58
  • I'm not sure what you're asking here. If you want to suppress displaying the timezone, don't add `zzz` to your `SimpleDateFormat`. – Mike Harris Aug 19 '14 at 17:03
  • @MikeHarris No I want it to read GMT not GMT+0.00 I am trying to avoid just removing +0.00 from the String. I am also trying to avoid etc/Greewich because people think it sounds too much like a location. People above me decided that it should be GMT instead of UTC – Jackie Aug 19 '14 at 17:37
  • Oh and "GMT" is considered a "abbreviation" and is thus deprecated according to docs. I will clean up the question once answered. – Jackie Aug 19 '14 at 17:39
  • 1
    This is probably not the cleanest solution, but since you're already calling `SimpleDateFormat.setTimezone()`, you might as well hard-code the timezone into your format string as well, e.g., `"dd-MMM-yyy kk:mm 'GMT'"` – Mike Harris Aug 25 '14 at 15:06
  • Please read https://stackoverflow.com/tags/timezone/info – Dhruv Kapatel Apr 03 '19 at 12:53

1 Answers1

1

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yes but that doesn't output zzz as expected. As I stated in my question it adds +0:00 which I would need to remove and I don't want to do that. – Jackie Aug 19 '14 at 15:00
  • @Jackie Please show the code you use to display the time. I don't think the format (i.e. whether +x:00 is shown) depends on the timezone you use. – Eran Aug 19 '14 at 15:02
  • Added more info for you. – Jackie Aug 19 '14 at 15:55