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

3

8

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(); }
Sarpedon answered 30/3, 2016 at 12:43 Comment(2)
As you can see your format doesn't fit the data that you wana parse MM, zzzz ? please read the documentationMonometallism
I recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use DateTimeFormatter and either OffsetDateTime or ZonedDateTime; all three are from java.time, the modern Java date and time API. See the answer by Arvind Kumar Avinash.Toogood
S
33

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);
Shawm answered 30/3, 2016 at 13:20 Comment(0)
G
1

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.

Gayegayel answered 28/3, 2021 at 17:37 Comment(0)
S
0

Wed Mar 30 00:00:00 GMT+05:30 2016
EEE MM dd kk:mm:ss zzzz yyyy

Your data doesn't match your pattern. To get it working, update your pattern to

"EEE MMM dd kk:mm:ss zXXX yyyy"

PS: here is a handy tool to test your patterns.

Sam answered 30/3, 2016 at 12:56 Comment(1)
thanks for the help. i had to use HH instead of kk. thanks for the tool as well. really appreciate it.Sarpedon

© 2022 - 2024 — McMap. All rights reserved.