B4A Library SPSntp - getting time from the internet

Hi again,

i took this sample code as a base to build this small library.

The aim was to receive the actual time from the internet in ticks (timezone GMT). I skipped the string conversion to EST so only the tick values are returned.

Dim sntp As SntpTime
Dim ticks As Long
sntp.NtpServerName = "0.de.pool.ntp.org"  ' only needed if you don't want to use the default server "0.us.pool.ntp.org"
sntp.TimeOut = 10000 ' only needed if you don't want to use the default timeout value of 30000 milliseconds (30 seconds)
ticks = sntp.getGmtNtpTime
Msgbox(ticks, "GMT Result in Ticks")
Msgbox(DateTime.Date(ticks), "GMT Date")
Msgbox(DateTime.Time(ticks), "GMT Time")
Msgbox(DateTime.Time(sntp.NtpTimeReference), "NtpTimeReference")
Msgbox(sntp.RoundTripTime, "RoundTripTime")
If an error occurrs during the time request (e.g. if a wrong server name was set), all time values return -1.

EDIT: Version 1.01 - added option to set an an own/preffered time server (default = "0.us.pool.ntp.org")
EDIT: Version 1.02 - the "android.permission.INTERNET" is now added automatically
EDIT: Version 1.03 - added option to set an own/preferred timeout value (default = 30000ms)

Caution: The way of handling network connections has changed since android 3 (you have to do all network connections within an non-gui background process) and this library does not support it directly. So if you wan't to use it with android 3 an above you have to handle the necessary threading on on your own.
Look at the attached sample "Sntp-Sample-V1.04".



  • SPSntp_V1.03.zip
    5.6 KB · Views: 646
  • SPSntp-Sample-1.04.zip
    7.5 KB · Views: 595
Last edited:


Licensed User
Longtime User

Many thanks I spent several hours searching for this page. In my opinion the documentation for B4x is particularly fragmented and as the forum grows it is more difficult to find good answers.

Actually I did find this documentation page in my searches but the library list is deep down and the page heading makes it look like a beginners tutorial.

Thanks for the library. I am most grateful.