Android Question Get datetime from a specific time zone.

jahswant

Well-Known Member
Licensed User
Longtime User
I have this timespan : 1726954050000 . I want to always display "Eastern Daylight Saving Time" independent of whic timezone i'm in.
 
Solution
To get a certain time in NY you need to know if DST is active.

Note 1: I can't fully test this sub since I'm in Toronto and we have the same time as NY.
Note 2: Bonus - the timezone java structure has other useful info.


B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Log("Associated time in NY: " & getTimeInNY(1726954050000))   '17:27:30
End Sub

Private Sub getTimeInNY(dt As Long) As String
    'get current time offsets in New York (EST-DST)
    Dim TimeZoneNY As JavaObject
    Dim TimeZoneNYStr As String = TimeZoneNY.InitializeStatic("java.util.TimeZone").RunMethod("getTimeZone", Array("America/New_York")).As(String)
    Dim tzParts() As String = Regex.Split("\,"...

William Lancee

Well-Known Member
Licensed User
Longtime User
To get a certain time in NY you need to know if DST is active.

Note 1: I can't fully test this sub since I'm in Toronto and we have the same time as NY.
Note 2: Bonus - the timezone java structure has other useful info.


B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Log("Associated time in NY: " & getTimeInNY(1726954050000))   '17:27:30
End Sub

Private Sub getTimeInNY(dt As Long) As String
    'get current time offsets in New York (EST-DST)
    Dim TimeZoneNY As JavaObject
    Dim TimeZoneNYStr As String = TimeZoneNY.InitializeStatic("java.util.TimeZone").RunMethod("getTimeZone", Array("America/New_York")).As(String)
    Dim tzParts() As String = Regex.Split("\,", TimeZoneNYStr)
   
'Just to see what info is available in this time zone structure
'    For i = 0 To tzParts.Length  - 1
'        Log(i &  TAB & tzParts(i))
'    Next

    Dim currentNYOffset As Long = tzParts(1).SubString(tzParts(1).IndexOf("=") + 1)
    Dim currentNYDST As Long = tzParts(2).SubString(tzParts(2).IndexOf("=") + 1)
    Dim currentNYOffset As Long = (currentNYOffset + currentNYDST) / 3600000
    Dim saveLocalOffset As Long = DateTime.TimeZoneOffset
    DateTime.SetTimeZone(currentNYOffset)
    Dim dtInNY As String = DateTime.Time(dt)
   
    'restore local offset
    DateTime.SetTimeZone(saveLocalOffset)
    Return dtInNY
End Sub
 
Upvote 1
Solution
Top