Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange date and time parsing result with SimpleDateFormat

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?

like image 578
Mavrik Avatar asked Nov 06 '10 15:11

Mavrik


1 Answers

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.

like image 115
BalusC Avatar answered Sep 28 '22 06:09

BalusC