Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat parse resets hours and minutes

I get a date as String like "start_date": "2013-07-05T11:32:00+00:00" and need a date object, which represents it with hours and minutes.

If I use the format "yyyy-MM-dd'T'HH:mm:ss+HH:mm" and sdf.parse(startDate) , in the returned date object the hours and minutes are resetted.

But I need also hours and minutes for date comparison. How can I workaround this problem ?

like image 421
Luser_k Avatar asked Dec 09 '22 14:12

Luser_k


2 Answers

You need the pattern "yyyy-MM-dd'T'HH:mm:ssXXX" to parse your date. ("XXX" stands for the timezone)

In your case the second "HH:mm" is resetting the values and using the last parsed data.

like image 80
Uwe Plonus Avatar answered Dec 11 '22 04:12

Uwe Plonus


This part is the problem : +HH:mm. You tell the parser that the part after the + sign is the hour and minutes part of the date, and these values are 00 and 00, so you get 0 and 0 as a result. The part startring at the + sign is in fact an offset to UTC time, and the symbol for that is XXX (since Java 7).

like image 25
JB Nizet Avatar answered Dec 11 '22 04:12

JB Nizet