Android Question Another unparseable date: "Wed, 28 Mar 2018 13:11:26 +0200"

rosippc64a

Active Member
Licensed User
Longtime User
Hi all!
Annoying error I got, read, but no solution I found.
Can somebody tell me where is the error?
thanks in advance
Steven


B4X:
Sub dateparse2( sd As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("java.util.Locale")
    jo.RunMethod("setDefault", Array As Object(jo.GetField("ENGLISH")))     
    Dim df As String = DateTime.DateFormat
    Dim res As Long
    Try
        DateTime.DateFormat = "EEE, d MMM yyyy HH:mm:ss ZZZ"
        res = DateTime.DateParse(sd.trim)
        DateTime.DateFormat = df
        Return DateTime.Date(res)
    Catch
        res = 0
        Log("Error parsing Date 2: " & sd)
        Return sd
    End Try
End Sub
 

Claudio Oliveira

Active Member
Licensed User
Longtime User
The problem is the weekday name.
Once you set "ENGLISH" as your default locale, the weekday name MUST be in english, otherwise you'll get this error.
Check this out and make sure the weekday is in english language.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
"EEE, d MMM yyyy HH:mm:ss ZZZ"

There is only one d for the day of the month. It must be "dd".
Also - provide the error log and it will be easy to find the problem.
 
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
Error occurred on line: 185 (MailParser)
java.text.ParseException: Unparseable date: "Wed, 28 Mar 2018 13:11:26 +0200"
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 anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at com.Misiolvas.mailparser._dateparse2(mailparser.java:221)
at com.Misiolvas.mailparser._parseheaders(mailparser.java:759)
at com.Misiolvas.mailparser._parsemail(mailparser.java:87)
at com.Misiolvas.email._pop_downloadcompleted(email.java:689)
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:342)
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$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

B4X:
Sub dateparse2( sd As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("java.util.Locale")
    jo.RunMethod("setDefault", Array As Object(jo.GetField("ENGLISH")))     
    Dim df As String = DateTime.DateFormat
    Dim res As Long
    Try
        DateTime.DateFormat = "EEE, dd MMM yyyy HH:mm:ss ZZZ"
        res = DateTime.DateParse(sd.trim) '<---- line 185
        DateTime.DateFormat = df
        Return DateTime.Date(res)
    Catch
        res = 0
        Log("Error parsing Date 2: " & sd)
        Return sd
    End Try
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tried this code and it works properly:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(dateparse2("Wed, 28 Mar 2018 13:11:26 +0200"))
End Sub

Sub dateparse2( sd As String) As String
   Dim jo As JavaObject
   jo.InitializeStatic("java.util.Locale")
   jo.RunMethod("setDefault", Array As Object(jo.GetField("ENGLISH")))
   Dim df As String = DateTime.DateFormat
   Dim res As Long
   Try
       DateTime.DateFormat = "EEE, d MMM yyyy HH:mm:ss ZZZ"
       res = DateTime.DateParse(sd.trim)
       DateTime.DateFormat = df
       Return DateTime.Date(res)
   Catch
       res = 0
       Log("Error parsing Date 2: " & sd)
       Return sd
   End Try
End Sub

Does it fail for you?
 
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
Yes, it fails to me. But, then maybe I have to beg your(s) pardon.
My test environment is an android emulator (android 8.0) where I take care to be installed language english (united kingdom and usa also) but there I always got exception.
I will have to check in real device.
 
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
Yes, i did it. The most weird is that, that I found other solution with AHdatetime lib, what works well (on the same android emulator):
B4X:
Sub dateparse( sd As String) As String
    'https://developer.android.com/reference/java/text/SimpleDateFormat.html
    'https://www.b4x.com/android/help/ahlocale.html
    Try
        Dim lang1 As AHLocale
        Dim dt As AHDateTime
        lang1.InitializeUS
        dt.Initialize2(lang1)
        If sd.Contains("GMT") Then
            dt.Pattern = "EEE, d MMM yyyy HH:mm:ss z"
        Else If sd.Contains("+") Or sd.Contains("-") Then '+0100
            dt.Pattern = "EEE, d MMM yyyy HH:mm:ss ZZZ"
        Else
            dt.Pattern = "EEE, d MMM yyyy HH:mm:ss"
        End If
        Dim myDate As Long = dt.Parse(sd) '("Mon, 13 Oct 201407:54:00 GMT")
        DateTime.DateFormat = "yyyy.MM.dd HH:mm:ss"
        dt.Initialize2(lang1)
        Log(DateTime.Date(myDate))
        sd = DateTime.Date(myDate)
        Return sd
    Catch
        Log(LastException.Message)
        Return sd
    End Try
End Sub
 
Upvote 0
Top