Android Question Finding a way to get device's timezone ID


Active Member
Licensed User
DateTime.TimeZoneOffset returns either -5 or -4 (Daylight saving Time) for New York and -6 or -5 (Daylight saving Time) for Chicago. As you can see, I could get -5 for both cities depending on whether Daylight Saving time is in effect and I have to handle the tricky Daylight Saving time related issue myself.

One solution is to use Timezone ID (such as "America/New_York" and "America/Chicago"). In my php code, I need to call date_default_timezone_set() function which expects a Timezone ID, i.e.,

I'm wondering what are some easy ways in B4A to get the phone device's Timezone ID.


Staff member
Licensed User
I can use the daylight saving time function to find out if it's in effect
What do you need it for? As I wrote, if you are using it for time calculations then you are most probably doing something wrong.

You can get the id string with:
Dim TimeZone As JavaObject
TimeZone = TimeZone.InitializeStatic("java.util.TimeZone").RunMethod("getDefault", Null)
Log(TimeZone.RunMethod("getID", Null))
Upvote 0


Active Member
Licensed User
In my app, I can't rely on the device's date and time because user can change it manually. So I'm getting the information from a ntp time server with a httpjob which passes the Timezone ID to the php script :
            Dim j As HttpJob
            j.Initialize("", Me)
            j.Download2("",Array As String("timezone_id", "America/Toronto")) 'get datetime from ntp server

php script ntpTime.php:
$timezone_id = $_GET["timezone_id"]; //"America/Toronto"
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);

date_default_timezone_set($timezone_id); //this is the only place Timezone ID is used

/* Query a time server (C) 1999-09-29, Ralf D. Kloth ( <ralf at> */
function query_time_server ($timeserver, $socket)
    $fp = fsockopen($timeserver,$socket,$err,$errstr,5);
        # parameters: server, socket, error code, error text, timeout
        fputs($fp, "\n");
        $timevalue = fread($fp, 49);
        fclose($fp); # close the connection
        $timevalue = " ";

    $ret = array();
    $ret[] = $timevalue;
    $ret[] = $err;     # error code
    $ret[] = $errstr;  # error text
} # function query_time_server

$timeserver =""; //""; // ""; //"";
$timercvd = query_time_server($timeserver, 37);

//if no error from query_time_server
    $timevalue = bin2hex($timercvd[0]);
    $timevalue = abs(HexDec('7fffffff') - HexDec($timevalue) - HexDec('7fffffff'));
    $tmestamp = $timevalue - 2208988800; # convert to UNIX epoch time stamp
    $datum = date("Y-m-d (D) H:i:s",$tmestamp - date("Z",$tmestamp)); /* incl time zone offset */
    $doy = (date("z",$tmestamp)+1);

   // echo "Time check from time server ",$timeserver," : [<font color=\"red\">",$timevalue,"</font>]";
    //echo " (seconds since 1900-01-01 00:00.00).<br>\n";
    //echo "The current date and universal time is ",$datum," UTC. ";
    //echo "It is day ",$doy," of this year.<br>\n";
    //echo "The unix epoch time stamp is $tmestamp.<br>\n";

    echo date("d/m/Y H:i:s", $tmestamp);

    echo "Unfortunately, the time server $timeserver could not be reached at this time. ";
    echo "$timercvd[1] $timercvd[2].<br>\n";
Upvote 0