Pergunta

I have a strange problem when parsing a ISO8601 date and time with SimpleDateFormat. The relevant code is:

public class DateHelper
{
    private static SimpleDateFormat iso8601formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    public static Date parseISO8601(String date) throws ParseException
    {
        Date result = iso8601formatter.parse(date);
        return result;
    }
}

For input I'm giving it a string

2010-09-06T15:30:00+02:00

And as a return I get a Date object with date set to 6th of January 2010 with time of 13:30 and timezone of GMT+00:00.

EDIT: I also tried using "2010-09-06T15:30:00+0200" with same results.

Confusing thing is, that the date set is partially correct, just the month is set wrongly.

The issue is shown on Android 1.6 and Android 2.2.

How can I fix it?

Foi útil?

Solução

Your problem is reproduceable if you use mm for month instead of MM.

So I suspect that the cause of the problem is there and that you're not running the version of the code you think you're running. Recompile and reexecute the code as you've in your question. It's correct.

Outras dicas

Probably not your problem but since SimpleDateFormat is not thread safe you need to change your code to:

public class DateHelper
{
    public static Date parseISO8601(String date) throws ParseException
    {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(date);
    }
}

Are you handling the ParseException? The code should be throwing a ParseException for that input string.
Not sure about Android, but with Java 6 the input string 2010-09-06T15:30:00+02:00 is not valid.

+02:00 is not a valid timezone.
Try it without the colon:

    2010-09-06T15:30:00+0200  

A trick (test) is to use the DateFormat to format a Date and check the created String:

    System.out.println(iso8601formatter.format(new Date()));  

I created a 1.6 project and the code worked for me...

public class DateHelper {
    private static SimpleDateFormat iso8601formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

public static Date parseISO8601(String date) throws ParseException {
    Date result = iso8601formatter.parse(date);
    return result;
}

public static String makeISO8601String(Date date) {
    return iso8601formatter.format(date);
}

}

public class StackOverflowTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    TextView textView = (TextView)this.findViewById(R.id.textView);

    StringBuilder builder = new StringBuilder();
    try {
        String badDateString = "2010-09-06T15:30:00+0200";
        Date parsedDate = DateHelper.parseISO8601(badDateString);
        builder.append(badDateString).append("\n");
        builder.append(parsedDate.toString()).append("\n");
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String dateString = DateHelper.makeISO8601String(new Date());
    builder.append("now="+dateString).append("\n");

    textView.setText(builder.toString());
}

}

For me it prints out:

2010-09-06T15:30:00+0200
Mon Sep 06 06:30:00 PDT 2010
now=2010-11-06T14:41:55-077

Which is all correct for me...

for parsing the first string u should use

public static Date parseDateFromString(String aDateString){
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    Date date=null;
    try {
        date= inputFormat.parse(aDateString);
    } catch (ParseException e) {
        return null;
    }
    return date;

when parsing the Timezone like +02:00 it is with z instead of Z. hope it helps. and the issue with giving you +00:00 i think is connected with the locale you use. u should try to test it with a different locale.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top