kungfuafrican
kungfuafrican

Reputation: 51

Simple question about java SimpleDateFormat

This will probably be a dumb question, but I don't understand the java date function. Here is some code:

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Date s = sdf.parse(var);
Calendar scal = java.util.GregorianCalendar.getInstance();
scal.setTime(s);   
Log.w("Time: ", Long.toString(s.getTime()));

If var = "10:00" I get "64800000".

If var = "11:00" I get "68400000".

If var = "12:00" I get "28800000".

If var = "13:00" I get "75600000".

If var = "14:00" I get "79200000".

If var = "00:00" I get "28800000".

What is up with 12:00? Why, when var=12:00 do I get the same result as when it's 00:00? All the other results seem correct. I obviously don't understand the java date function, but I can't seem to find any explanation for this anywhere. This is screwing up my time span calculator.

Upvotes: 5

Views: 3813

Answers (2)

kungfuafrican
kungfuafrican

Reputation: 51

I should have used "H" instead of "h". "h" is for am/pm format.

Upvotes: 0

GriffeyDog
GriffeyDog

Reputation: 8376

If you want to use 24-hour time, you need to use the capital HH format:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 

Upvotes: 12

Related Questions