Android Question B4A - Unparseable Date "20180311"

Ross Woodward

Member
Licensed User
Longtime User
Hi There,

I'm having issues trying to convert a string to a date.

I have found the following post - however that didn't help.

https://www.b4x.com/android/forum/threads/unparseable-date.15544/#post-240642

My Code :

B4X:
        Dim sDate As String
        Dim sHolder As String
        sHolder = Value
        Dim sLenght As Int
        sLenght = sHolder.Length - 8
        sDate = sHolder.SubString(sLenght)
        DateTime.DateFormat = "dd-MM-yyyy"
        Dim t As Long = DateTime.DateParse(sDate)

a Spinner [combo box] has a value which the last 8 characters is a date, which I'm then trying to convert.

I get error :

B4X:
java.text.ParseException: Unparseable date: "20180313"
    at java.text.DateFormat.parse(DateFormat.java:362)
    at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:148)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$1.run(BA.java:325)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6809)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Hope you can help.
 

DonManfred

Expert
Licensed User
Longtime User
Based on the Inputdate (20180303) you should use a Dateformat of
B4X:
DateTime.DateFormat = "yyyyMMdd"
        Dim t As Long = DateTime.DateParse(sDate)
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
Based on the Inputdate (20180303) you should use a Dateformat of
B4X:
DateTime.DateFormat = "yyyyMMdd"
        Dim t As Long = DateTime.DateParse(sDate)

Thanks -

However, I forgot to mention I need the string to be format in "yyyy-MM-dd" for my web service to recognise it.
Thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to be consistent in the date formats.
You can switch between date formats like.
B4X:
Private sDate = "20180313" As String
DateTime.DateFormat = "yyyyMMdd"
Dim tDate As Long = DateTime.DateParse(sDate)
DateTime.DateFormat = "yyyy-MM-dd"
Log(DateTime.Date(tDate))
Returns 2018-03-13
 
Upvote 0

Ross Woodward

Member
Licensed User
Longtime User
You need to be consistent in the date formats.
You can switch between date formats like.
B4X:
Private sDate = "20180313" As String
DateTime.DateFormat = "yyyyMMdd"
Dim tDate As Long = DateTime.DateParse(sDate)
DateTime.DateFormat = "yyyy-MM-dd"
Log(DateTime.Date(tDate))
Returns 2018-03-13

Thanks - All resolved.
 
Upvote 0

Similar Threads

Replies
3
Views
1K
Replies
1
Views
7K
Replies
7
Views
2K
Top