开发者

Simple question about java SimpleDateFormat

开发者 https://www.devze.com 2023-02-03 11:41 出处:网络
This will probably be a dumb question, but I don\'t understand the java date function. Here is some code:

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 explana开发者_如何转开发tion for this anywhere. This is screwing up my time span calculator.


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

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消