Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get timezone in "+0100" (for example) format

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.

like image 200
Cath Avatar asked Dec 16 '22 00:12

Cath


1 Answers

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);
like image 192
Cath Avatar answered Jan 03 '23 15:01

Cath