Ron Jobbins Ron Jobbins - 2 years ago 233
Java Question

Difference between kk and HH in date formatting java

I'm pretty new to java and am trying to format a time using 24 hour format. I've come across two ways of formatting the hour - HH and kk:

SimpleDateFormat format1 new SimpleDateFormat("HH:mm");
SimpleDateFormat format2 new SimpleDateFormat("kk:mm");
Date date = new Date();

These both produce something like 11:21. What's the difference between them? Am I missing something?

Answer Source

The two formats essentially do the same thing but differ in how they handle midnight. kk will format midnight to 24:00 whereas HH will format to 00:00. The hours in a day in k are 1-24 and in H are 0-23

It's always worth checking the java documentation as it generally provides very useful explanations as well as examples of uses.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download