Search code examples
androidsimpledateformatandroid-6.0-marshmallow

Unparseable date: "Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)" (at offset 39) in Android 6.0


Im trying parse a String with a date to convert it into a Date format. Strings are in the following format.

Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)

SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, MMM dd yyyy; hh:mm:ss a",Locale.ENGLISH);

for(int i=0 ; i <jArr.length() ; i++){
    String tempDate = jArr.get(i).toString();
    dateList.add(tempDate);
}

try{
    Date d1 = sdf3.parse(dateList.get(0));                        
} catch (Exception e) {
    e.printStackTrace(); 
}

Note: This function is working fine for android version > 6.


Solution

  • To get your desired format you have to use SimpleDateFormat like below:

    String dateString = "Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)";
    SimpleDateFormat sourceFormat = new SimpleDateFormat("EEE, MMM dd yyyy; hh:mm:ss a",Locale.ENGLISH);
    
    try{
        Date d1 = sourceFormat.parse(dateString);                        
    } catch (Exception e) {
        e.printStackTrace(); 
    }
    
    SimpleDateFormat targetFormat = new SimpleDateFormat("dd/MM/yy; hh:mm:ss a", Locale.ENGLISH)
    
    String desiredString = targetFormat.format(d1);
    //desiredString is now "09/01/20; 04:31:59 PM"