6

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.

Quasdunk
  • 14,944
  • 3
  • 36
  • 45
theanimashaun
  • 306
  • 1
  • 2
  • 12
  • 1
    Can you please post the actual source and the stacktrace? – Gyro Gearless Oct 08 '13 at 12:01
  • Welcome to SO. As @GyroGearless says, please consider add your code and full stacktrace. This information is always useful to help you. – dic19 Oct 08 '13 at 12:06
  • From the exception it looks like you are trying to parse an empty string. Check the string before the parse function. – sjdutta Oct 08 '13 at 12:10
  • Your timestamp is a ISO compliant timestamp, this answer gives you more details: http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date – DArkO Oct 08 '13 at 11:52

2 Answers2

2

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;
    }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
-2

If you use java 7, you can use:

yyyy-MM-dd'T'HH:mm:ssXXX

You can check more here

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61