Android Question Date Format

Almora

Well-Known Member
Licensed User
Longtime User
hi
I can't get output. where am i doing wrong?
thanks.

B4X:
    ................ 
   Dim pubDate As String = item.Get("pubDate")
     ......................
   pubDate="Fri, 24 Dec 2021 20:59:36 GMT"
   ................
 
    DateTime.DateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
    Dim lDate As Long = DateTime.DateParse(pubDate)
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
    Dim sDate As String = DateTime.Date(lDate)
    Log(sDate)

[QUOTE]
java.text.ParseException: Unparseable date: "Fri, 24 Dec 2021 20:59:36 GMT"
[/QUOTE]
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I copied your code above, and it works fine for me.
B4X:
Sub Button1_Click
    Dim pubDate As String' = item.Get("pubDate")
    pubDate="Fri, 24 Dec 2021 20:59:36 GMT"
 
    DateTime.DateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
    Dim lDate As Long = DateTime.DateParse(pubDate)
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
    Dim sDate As String = DateTime.Date(lDate)
    Log(sDate)
End Sub
 
Upvote 0

Almora

Well-Known Member
Licensed User
Longtime User
i try but this error..

main_button1_click (java line: 404)
java.text.ParseException: Unparseable date: "Fri, 24 Dec 2021 20:59:36 GMT"
at java.text.DateFormat.parse(DateFormat.java:358)
at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:148)
at b4a.exampled.main._button1_click(main.java:404)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6161)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This works:
B4X:
pubDate="Fri, 24 Dec 2021 20:59:36 GMT" 
    DateTime.DateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"   'lower or upper Z
    Dim lDate As Long = DateTime.DateParse(pubDate)
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
    Dim sDate As String = DateTime.Date(lDate)
    Log(sDate)   '12/24/2021 15:59:36
 
Upvote 0

Almora

Well-Known Member
Licensed User
Longtime User
didn't work
B4X:
pubDate="Fri, 24 Dec 2021 20:59:36 GMT"
    DateTime.DateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"   'lower or upper Z
    Dim lDate As Long = DateTime.DateParse(pubDate)
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
    Dim sDate As String = DateTime.Date(lDate)
    Log(sDate)   '12/24/2021 15:59:36


this work

B4X:
    Dim jo As JavaObject
    jo.InitializeStatic("java.util.Locale").RunMethod("setDefault", Array(jo.GetField("US")))

    Dim pubDate As String' = item.Get("pubDate")
    pubDate="Fri, 24 Dec 2021 20:59:36 GMT"
 
    DateTime.DateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
    Dim lDate As Long = DateTime.DateParse(pubDate)
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
    Dim sDate As String = DateTime.Date(lDate)
    Log(sDate)

12/24/2021 15:59:36
 
Upvote 1

AnandGupta

Expert
Licensed User
Longtime User
I had similar code but upper Z gave error and worked in lower z.
Try it.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Just to clarify.
The problem is 'Fri' and 'Dec' in the date.
This works only when the device language is set to English.
On my device, the code in post #1 works OK if I replace 'Fri' by 'ven' and 'Dec' by 'déc' because my device language is set to French.
The solution in post #6 was to set the language to English.
 
Upvote 0
Top