Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.text.ParseException: Unparseable date: "" (at offset 0) [duplicate]

Tags:

java

android

I've read many answers to this problem but no answer solves my problem

I am trying to parse this string :

"2013-10-07T23:21:00+01:00"

to a Date object with the simpledateformat :

"yyyy-MM-dd'T'HH:mm:ssZZZZZ"

but it keeps producing the error:

java.text.ParseException: Unparseable date: "" (at offset 0)

Note: I am trying this on Android, I'm a beginner.

like image 457
theanimashaun Avatar asked Oct 08 '13 11:10

theanimashaun


1 Answers

Try with following code

public static Calendar parseDate(String dateTimeStr)
            throws ParseException {
        Calendar calendar = GregorianCalendar.getInstance();
        String s = dateTimeStr.replace("Z", "+00:00");
        try {
            s = s.substring(0, 22) + s.substring(23);
        } catch (IndexOutOfBoundsException e) {
            throw new ParseException("Invalid length", 0);
        }
        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s);
        calendar.setTime(date);
        return calendar;
    }
like image 200
Sunil Kumar Sahoo Avatar answered Nov 15 '22 16:11

Sunil Kumar Sahoo