Android Question Date time parse [SOLVED]

khwarizmi

Active Member
Licensed User
Longtime User
Hello all

How can I parse a date-time in the formula :
B4X:
2021-01-08T11:39:00.000Z

thanks
 

khwarizmi

Active Member
Licensed User
Longtime User
this is input, I want to parse this date string to year, month, day .. etc
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How can I parse a date-time in the formula :
To parse:
2021-01-08T11:39:00.000
You do this:
B4X:
DateTime.DateFormat="yyyy-MM-dd'T'HH:mm:ss.000"
    Log(DateTime.DateParse("2021-01-08T11:39:00.000"))
not sure about your Z for your time zone in your date.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    DateTime.DateFormat = "yyyy-MM-dd"
    'DateTime.TimeFormat = "hh:mm:ss"
    Dim InputDate() As String = Regex.Split("T", "2021-01-08T11:39:00.000Z")
    Dim t1 As Long = DateTime.DateParse(InputDate(0))
    Dim t2 As Long = DateTime.TimeParse(InputDate(1))
    
    Log(DateTime.Date(t1))
    Log(DateTime.GetYear(t1))
    Log(DateTime.GetMonth(t1))
    Log(DateTime.GetDayOfMonth(t1))
    Log(DateTime.Time(t2))
    Log(DateTime.GetHour(t2))
    Log(DateTime.GetMinute(t2))
    Log(DateTime.GetSecond(t2))
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I want to parse this date string to year, month, day .. etc
Normally, when you parse a date you are converting the date string into ticks, but if all you are interested in is breaking up the date string into its elements, in this case you do not need to use any of the datetime methods. Keep it simple like this:
B4X:
Dim d As String ="2021-01-08T11:39:00.000Z".Replace("-",":").Replace("T",":").Replace("Z","")
    Dim str() As String=Regex.Split(":",d)
    For i=0 To str.Length-1
        Log(str(i)) 'logs all 6 components
    Next
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
Thanks everyone for the help, I was confused about the symbols T and Z and how to handle them, they could just be ignored in the string or replaced with a blank.
I found at stackoverflow:


"The T is just a literal to separate the date from the time, and the Z means "zero hour offset" also known as "Zulu time" (UTC). If your strings always have a "Z" you can use:

SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));

Or using Joda Time, you can use ISODateTimeFormat.dateTime(). "
 
Upvote 0
Top