Why shouldn't I use shortcuts in Java TimeZone

The main problem: "GMT" TimeZone returns things at its discretion. "etc / GMT" returns with an additional "+0: 00", which is undesirable. "etc / Greenwich" seems to confuse people that they relate to a specific location, this is undesirable.

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

Original publication

In Java Doc

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

I'm trying to use this, so I went to "etc / GMT", but when I use the output, it looks like ...

But I do not want too much +0.00. If I use the abbreviation, it does not show +0.00, etc. / Greenwich. Both of these options seem to be bad, because one is clearly incorrect given the document (GMT), and one of them is too location specific (etc / greenwich). Another crop option of +0.00 is also not a good solution.

Why abbreviations are no longer supported, and what is the correct TimeZone if I do not want +0.00

Using Java 1.7.51 (also 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)

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 it here ...

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

And it works as we would like. When will I be back to run locally using

java version "1.7.0_51" Java (TM) SE Runtime Environment (build 1.7.0_51-b13) 64-bit Java HotSpot TM server virtual machine (build 24.51-b03, mixed mode)

I see...

19-aug-2014 15:53 ​​GMT + 00: 00

0
source share
1 answer

Abbreviations are not recommended because they are not unique. For example, EST means both Eastern Standard Time in the United States and eastern Australia.

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

Etc / GMT
Etc / UTC
Etc / Universal

Please note that these are not abbreviations. These are full names.

+1
source

Source: https://habr.com/ru/post/1262876/


All Articles