Get the Current Timezone?

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
piz
Posts: 25
Joined: 13 Dec 2017 19:56

Get the Current Timezone?

Post by piz » 04 Jan 2019 19:33

Is it possible to get the timezone I'm currently in? I need to convert local times to UTC and I might be in any timezone when that happens. The system knows the current timezone - does Automagic have a way to get it?

Thanks!

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Get the Current Timezone?

Post by anuraag » 04 Jan 2019 22:30

My timezone is IST.

Code: Select all

 tz="{0, dateformat, hh:mm}" //05:30

Code: Select all

 tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName()"); //India Standard Time

Code: Select all

 tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName(boolean, int)", false, getJavaStaticField("java.util.TimeZone", "SHORT")); //IST

Code: Select all

 tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getID()"); //Asia/Kolkata
To get current time in UTC

Code: Select all

 {getDate(),dateformat,timezone,UTC,HH:mm}"

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Get the Current Timezone?

Post by Desmanto » 05 Jan 2019 06:21

anuraag wrote:

Code: Select all

 tz="{0, dateformat, hh:mm}" //05:30
Ah, thanks. This is much shorter. If I only need to get the hour, I simply change the hh:mm to h only. But this is risky for timezone that has 30 minutes.

I used to use this

Code: Select all

tz = -getDate("{0,dateformat,z}", "z");
Which will get the miliseconds instead, can be used for add/subtract operation directly. But still have to be divided by 3600000 to get the hour.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
piz
Posts: 25
Joined: 13 Dec 2017 19:56

Re: Get the Current Timezone?

Post by piz » 05 Jan 2019 14:51

Thanks, anuraag, those are perfect, especially this one:
anuraag wrote:

Code: Select all

tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName(boolean, int)", false, getJavaStaticField("java.util.TimeZone", "SHORT")); //IST

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Get the Current Timezone?

Post by anuraag » 05 Jan 2019 15:01

piz wrote:Thanks, anuraag, those are perfect, especially this one:
anuraag wrote:

Code: Select all

tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName(boolean, int)", false, getJavaStaticField("java.util.TimeZone", "SHORT")); //IST
Alternative posted by Desmanto If you need that only

Code: Select all

tz = "{0,dateformat,z}" //IST 

Post Reply