ParseException while parsing a valid date

I am getting reports of two Android clients having problems parsing the HTTP response date. I am logging the date the client is trying to parse and that's ok.

The block that parses the date:

    public Date parseDateInHeader(String dateHeader) throws ParseException {
       SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss 'GMT'");
       simpledateformat.setTimeZone(TimeZone.getTimeZone("GMT"));
       return simpledateformat.parse(dateHeader);
   }

      

The exception being logged in my crash reporting system:

Caused by: java.text.ParseException: Unparseable date: "Thu, 08 Jan 2015 21:16:44 GMT" (at offset 0)
   at java.text.DateFormat.parse(DateFormat.java:555)
   at com.mycompany.api.util.DateUtils.parseDateInHeader(DateUtils.java:45)
   at com.mycompany.api.network.procedure.NetworkProcedure.getResponseHeaders(NetworkProcedure.java:54)
   at com.mycompany.api.network.procedure.GetNetworkProcedure.run(GetNetworkProcedure.java:58)
   at com.mycompany.api.network.procedure.GetNetworkProcedure.run(GetNetworkProcedure.java:43)
   at com.mycompany.api.network.procedure.Procedure.checkedRun(Procedure.java:26)
   at com.mycompany.api.impl.BaseMendeleySdk.getDocumentTypes(BaseMendeleySdk.java:235)
   at com.mycompany.sync.SyncRequest.syncDocumentTypes(SyncRequest.java:80)
   at com.mycompany.sync.SyncRequest.sync(SyncRequest.java:57)
   at com.mycompany.sync.SyncControllerImpl$1.doInBackground(SyncControllerImpl.java:82)
   at com.mycompany.sync.SyncControllerImpl$1.doInBackground(SyncControllerImpl.java:77)
   at android.os.AsyncTask$2.call(AsyncTask.java:288)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:841)

      

  • As you can see, the string I am processing is absolutely correct and follows the pattern set for SimpleDateFormat: "Thu, 08 Jan 2015 21:16:44 GMT"
  • This only happens for two out of hundreds in production. One is Samsung Galaxy Note 8 and the other is Samsung Galaxy 3.
  • The date comes from the HTTP request response header. The HTTP server is the same used by the rest of the clients and it only fails with these two devices.

So this is the great mystery. Any ideas?

+3


source to share


2 answers


You need to pass the appropriate Locale in this case



+1


source


The problem is that SimpleDateFormat is using the phone locale for parsing and I did not specify Locate in the SimpleDateFormat constructor, so it was taking the Device Locale which is not compatible with the date format.

The way to solve it is to pass US as Locale in the SimpleDateFormat constructor, since the HTTP server prints the day of the week in English:

SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss 'GMT'", Locale.US);

      



The device is in Spanish, and the Spanish language "Thu" does not exist for a week.

Solved thanks to Michael Conicez answer here

+1


source







All Articles