Sammy Guergachi
Sammy Guergachi

Reputation: 2006

SimpleDateFormat Parses and Compares time incorrectly

When using the following SimpleDateFormat:

 SimpleDateFormat format = new SimpleDateFormat("hh:mm");

Then I parse then compare 2 values: 12:19 and 11:40 like so:

val = format.parse("12:19").compareTo(format.parse("11:40"));

I get val to be -1, which seems wrong since 12hrs and 19mins is greater than 11hrs and 40mins

However when I change the first value to 19:19 and compare it again to 11:40 then val returns a value of 1 which seems to be correct.

Not sure why this is, I think i'm missing something.

Upvotes: 0

Views: 45

Answers (1)

Simon Arsenault
Simon Arsenault

Reputation: 1845

12:19 is interpreted as 00:19 because of "hh". You need "HH".

hh is 1-12 format.

HH is 0-23 format.

Read more here.

Upvotes: 5

Related Questions