B4J Question Parse DateTime string

EvgenyB4A

Active Member
Licensed User
Longtime User
Excuse me for long time response.
I have currentTime":"2023-04-18T05:03:55.226Z"

dt_long = 1681783435226
DateTime.Date(dt_long) = 2023-04-18T05:03:55.226Z

No time zone!
When I use DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" I get: "Error: (ParseException) java.text.ParseException: Unparseable date: "2023-04-18T05:18:19.994Z"
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You need to set the timezone yourself.

 
Upvote 0

teddybear

Well-Known Member
Licensed User
I use DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" and get the time without time zone data.

Z or X , no single quotes

B4X:
DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
or
DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
    DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"$    ' <-- OK
    'DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSSZ"$     ' <-- Error
    Dim dt_long As Long = DateTime.DateParse("2023-04-18T14:45:37.503Z")
    Log(dt_long)
    Log(DateTime.Date(dt_long))
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
    DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"$    ' <-- OK
    'DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSSZ"$     ' <-- Error
    Dim dt_long As Long = DateTime.DateParse("2023-04-18T14:45:37.503Z")
    Log(dt_long)
    Log(DateTime.Date(dt_long))
In DateFormat String, the strings enclosed with single quotes will be treated as original strings and not parsed, it will display 2023-04-18T14:45:37.503Z without zone
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
The format cannot use for parsing as Long but can be use to format the date as String.
B4X:
DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"$    ' <-- OK
'DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSSZ"$     ' <-- Error when Parsing
Dim dt_long As Long = DateTime.DateParse("2023-04-18T15:13:37.503Z")
Log(dt_long)
Log(DateTime.Date(dt_long))
DateTime.DateFormat = $"yyyy-MM-dd'T'HH:mm:ss.SSSZ"$     ' <-- OK when formatting
Log(DateTime.Date(dt_long))
 
Upvote 0
Top