B4J Question help with parsing time

Blueforcer

Well-Known Member
Licensed User
Longtime User
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
The date-time format is the ISO 8601 format.
...and according to the stackoverflow answer here, you can not parse it fully using SimpleDateFormat.

... i dont know what 1510Z means
I think the 1510 must be further fractions of a second. ISO8601 has 9 (up to nanoseconds) in the post I link above, so I'm not sure why you just have 7
The Z just denotes Zulu time (UTC).
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
If you don't need the full accuracy, and milliseconds are enough, you could just substring it...
B4X:
DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
Dim strTime As String = "2022-06-06T12:45:23.4651510Z"
Dim milli As String = strTime.SubString2(0, 23)
Log(milli)
Dim LngDate As Long = DateTime.DateParse(milli)
Log(DateTime.Date(LngDate))
If you need the full accuracy, perhaps there's a way to use the java.time package mentioned in that stackoverflow answer?
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
If you don't need the full accuracy, and milliseconds are enough, you could just substring it...
B4X:
DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
Dim strTime As String = "2022-06-06T12:45:23.4651510Z"
Dim milli As String = strTime.SubString2(0, 23)
Log(milli)
Dim LngDate As Long = DateTime.DateParse(milli)
Log(DateTime.Date(LngDate))
If you need the full accuracy, perhaps there's a way to use the java.time package mentioned in that stackoverflow answer?
Thank you very much
Its for a racing tracker and therefore it needs to be as accurate as possible
Does B4A provide a function to parse it completely?
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Given that java.time has apparently been around since java 8, I guess that it can be accessed with JavaObject.

That is beyond my knowledge I'm afraid though! We'll need someone who can understand the API and how to access it.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Do you need an accuracy better than the second ?
I am afraid that you cannot get better results.
From what I have seen the B4X DateTime methods truncate the milliseconds.
I have tested quite some different possibilities.

Below my test routine:

B4X:
Private Sub CheckTime
    Private Timestamp As String
    Private Date1, Time1 As String
    Private it As Int
    Private DateTimeTicks As Long
    
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    Dim LngDate As Long = DateTime.DateParse("2022-05-06T12:45:23.4651510Z")
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSS"
    Log(DateTime.Date(LngDate))
    Log(DateTime.Time(LngDate))
    Log(LngDate)
    
    Timestamp = "2022-05-06T12:45:23.4651510Z"
    it = Timestamp.IndexOf("T")
    DateTime.DateFormat = "yyyy-MM-dd"
    DateTime.TimeFormat = "HH:mm:ss"
    Date1 = Timestamp.SubString2(0, it)
    Time1 = Timestamp.SubString2(it + 1, it + 9)
    Log(Date1)
    Log(Time1)
    DateTimeTicks = DateTime.DateTimeParse(Date1, Time1)
    Log(DateTimeTicks)
    Log(DateTime.Date(DateTimeTicks))
    Log(DateTime.Time(DateTimeTicks))
End Sub
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Based on the link in post #6 and the details of the java.time.Instant method, I've come up with the following.
Please bear in mind that this is my first attempt at using JavaObject without help, so I may be leading you down the wrong path....

The range of an instant requires the storage of a number larger than a long. To achieve this, the class stores a long representing epoch-seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999. The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z where instants after the epoch have positive values, and earlier instants have negative values. For both the epoch-second and nanosecond parts, a larger value is always later on the time-line than a smaller value.
B4X:
Dim jo As JavaObject
jo.InitializeStatic("java.time.Instant")
Dim instant As JavaObject = jo.RunMethod("parse", Array As Object("2022-06-06T12:45:23.4651510Z"))
Dim epochSecond As Long = instant.RunMethod("getEpochSecond", Null)
Dim nanoSecs As Int = instant.RunMethod("getNano", Null)
    
Log(epochSecond)    '1654519523
Log(nanoSecs)        '465151000
 
Upvote 0
Top