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.

4
  • Your timestamp is a ISO compliant timestamp, this answer gives you more details: stackoverflow.com/questions/2201925/… Commented Oct 8, 2013 at 11:52
  • 1
    Can you please post the actual source and the stacktrace? Commented Oct 8, 2013 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. Commented Oct 8, 2013 at 12:06
  • From the exception it looks like you are trying to parse an empty string. Check the string before the parse function. Commented Oct 8, 2013 at 12:10

2 Answers 2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

-2

If you use java 7, you can use:

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

You can check more here

2 Comments

The 'X' is available since Java 7.
Android doesn't support Java 7, unfortunately

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.