Java Simple Date Format Time Issues

SimpleDateFormat sdf1 = new SimpleDateFormat(yyyy-MM-dd kk:mm:ss);
SimpleDateFormat sdf2 = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
Calendar cal = Calendar.getInstance();
Date d = cal.getTime();
System.out.println("Current Time is:"+d);
System.out.println("Time value using kk:" + sdf1.format(d));
System.out.println("Time Value using HH:" + sdf2.format(d));

Result:
Current Time is: Wed Sep 25 00:55:20 IST 2013
Time value using kk : 2013-09-25 24:55:20
Time value using HH : 2013-09-25 00:55:20

Can anyone tell why this behavior changes in the time, when using kk and HH.

and also kk gives 24:55:20, is this be useful any where. To my knowledge, there is only 00:00:00 to 23:59:59 is the time range.

Is there any beyond this range, if so where is the place "kk" will be useful?

Jon Skeet
people
quotationmark

There's no reason why there should be an error. k is documentedin SimpleDateFormat as a field for:

Hour in day (1-24)

That explains why you get 24 for midnight instead of 00, too. If you don't want values between 01 and 24, don't use kk in your format string! I can't remember the last time I wanted to use something like that, but presumably it's used in some cases (particularly for values which are always "on the hour" so you get 24:00 instead of 00:00).

Always consult the docs for what format strings mean.

people

See more on this question at Stackoverflow