Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
System.out.println(format1.format(date));
System.out.println(format2.format(date));

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

like image 385
Ron Jobbins Avatar asked Apr 03 '14 10:04

Ron Jobbins


People also ask

What is KK date?

Krishnakumar Kunnath, better known as KK, was born in Delhi, India, on August 23, 1968. Mount St. Mary's School in Delhi was where he received his primary education and where he grew up.

What is HH date format?

The hour, using a 12-hour clock from 01 to 12. More information: The "hh" Custom Format Specifier. The hour, using a 24-hour clock from 0 to 23. More information: The "H" Custom Format Specifier.

What is HH in Java?

The “HH” format in Java Date is like 00, 01, 02, 03, … 23 hour. Use SimpleDateFormat("HH") to get the same format and in that way you can format hour. // displaying hour in HH format SimpleDateFormat simpleformat = new SimpleDateFormat("HH"); String strHour = simpleformat.


2 Answers

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.

like image 124
Josh Roberts Avatar answered Nov 15 '22 15:11

Josh Roberts


try this to see the difference

    SimpleDateFormat format1 =  new SimpleDateFormat("HH:mm");
    SimpleDateFormat format2 = new SimpleDateFormat("kk:mm");
    Date date = new GregorianCalendar(2001, 0, 1, 0, 0 , 0 ).getTime();
    System.out.println(format1.format(date));
    System.out.println(format2.format(date));

output

00:00
24:00
like image 42
Evgeniy Dorofeev Avatar answered Nov 15 '22 14:11

Evgeniy Dorofeev