4

I got lost between the documentation and the numerous time-related questions found by google.

What I want is very simple: a string that represents the running timezone in the "[+/-] [number] [number] [number] [number]" format (for instance "+0100").

My current code is:

    Calendar cal = Calendar.getInstance();
    TimeZone tz = cal.getTimeZone();
    String gmt = "" + tz.getID();

which returns "GMT" in my location, which I assume is the short version (3 letters) of the timezones.

Cath
  • 135
  • 2
  • 9
  • For new readers to the question I recommend you don’t use `Calendar` and `TimeZone`. Those classes are poorly designed and long outdated. Instead use `ZonedDateTime` and `ZoneId`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 08 '21 at 00:10

4 Answers4

7

Here is the code I actually ended up using, I hope it helps someone.

Date today = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("Z");
String gmt = sdf.format(today);
Cath
  • 135
  • 2
  • 9
3

You should try following thing.

Calendar c = Calendar.getInstance();
        System.out.println("current: " + c.getTime());

        TimeZone z = c.getTimeZone();
        int offset = z.getRawOffset();
        if (z.inDaylightTime(new Date())) {
            offset = offset + z.getDSTSavings();
        }
        int offsetHrs = offset / 1000 / 60 / 60;
        int offsetMins = offset / 1000 / 60 % 60;

        System.out.println("offset: " + offsetHrs);
        System.out.println("offset: " + offsetMins);

        c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
        c.add(Calendar.MINUTE, (-offsetMins));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("GMT Time: " + sdf.format(c.getTime()) + " +"
                + offsetHrs + ":" + offsetMins);

OUTPUT

current: Wed May 16 22:32:14 IST 2012
offset: 5
offset: 30
GMT Time: 2012-05-16 17:02:14 +5:30
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • Thank you for your fast answer. Actually I do not need the date (which is defined by the user in the context of a graph request), just the GMT code. :) I have one question, though: what if the timezone is "negative"? – Cath May 16 '12 at 17:17
  • Then it will be displayed in negative format. And you can get the timezone value from `offsetHrs` and `offsetMins`. – Bhavik Ambani May 16 '12 at 17:18
1

It seems like you want "Z" or "X" in SimpleDateFormat

Z   Time zone   RFC 822 time zone   -0800
X   Time zone   ISO 8601 time zone  -08; -0800; -08:00
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I actually liked your approach, but it would have been nice if you've showed me some alternative code. I will add my current code in the answer. – Cath May 17 '12 at 14:15
1

java.time

The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using the modern API:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Replace ZoneId as applicable e.g. ZoneId.of("America/New_York")
        ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
        
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("XX", Locale.ENGLISH);
        String offset = dtf.format(now);
        System.out.println(offset);
    }
}

Output:

+0100

Learn more about the the modern date-time API* from Trail: Date Time.


Feedback from Ole V.V.:

You may want to use lower case xx so you also get +0000 rather than Z for offset zero.

Thanks, Ole V.V. for the value addition which will benefit someone looking for +0000 rather than Z for a zero-timezone offset. I recommend using this strictly based on the specific requirement. ISO 8601 recommends using Z as the timezone designator for a zero-timezone offset.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110