Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ParseException: Unparseable date: "Wed Mar 30 00:00:00 GMT+05:30 2016" (at offset 4)

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

Wed Mar 30 00:00:00 GMT+05:30 2016

But when im parsing the String i get a error saying,

java.text.ParseException: Unparseable date: "Wed Mar 30 00:00:00 GMT+05:30 2016" (at offset 4)

below is a part of my code. How do i avoid this error? Any help would be appreciated.

SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MM dd kk:mm:ss zzzz yyyy",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(); }
like image 866
Ryan Aluvihare Avatar asked Mar 30 '16 12:03

Ryan Aluvihare


2 Answers

Check this once. working fine for me

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

    Date d1 = null;
    try{
        d1 = sdf3.parse("Wed Mar 30 00:00:00 GMT+05:30 2016");

    }catch (Exception e){ e.printStackTrace(); }


    System.out.println("check..." + d1);
like image 55
Satya siva prasad Avatar answered Nov 15 '22 10:11

Satya siva prasad


java.time

The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API* .

Using modern date-time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Wed Mar 30 00:00:00 GMT+05:30 2016";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtf);
        System.out.println(odt);
    }
}

Output:

2016-03-30T00:00+05:30

Learn more about the modern date-time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

like image 34
Arvind Kumar Avinash Avatar answered Nov 15 '22 10:11

Arvind Kumar Avinash