Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between java HH:mm and hh:mm on SimpleDateFormat

Tags:

java

date

format

Whats the difference between kk:mm, HH:mm and hh:mm formats ??

    SimpleDateFormat broken = new SimpleDateFormat("kk:mm:ss");     broken.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));     SimpleDateFormat working = new SimpleDateFormat("HH:mm:ss");     working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));     SimpleDateFormat working2 = new SimpleDateFormat("hh:mm:ss");     working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));      System.out.println(broken.format(epoch));     System.out.println(working.format(epoch));     System.out.println(working2.format(epoch)); 

prints:

24:00:00 00:00:00 05:30:00 
like image 767
user2527039 Avatar asked Jun 27 '13 10:06

user2527039


People also ask

What is the difference between HH mm SS and HH mm SS?

[h]:mm:ss shows the total number of hours, even if the number of hours is 24 or more. hh:mm:ss effectively only shows the excess hours over and above complete multiples of 24. In your example, 105 hours is 4x24, or 4 whole days, plus 9 more hours, so it just shows the 9.

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.

What is the format of SimpleDateFormat?

Class SimpleDateFormat. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

What is the difference between DateTimeFormatter and SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.


2 Answers

kk: (01-24) will look like 01, 02..24.

HH:(00-23) will look like 00, 01..23.

hh:(01-12 in AM/PM) will look like 01, 02..12.

so the last printout (working2) is a bit weird. It should say 12:00:00 (edit: if you were setting the working2 timezone and format, which (as kdagli pointed out) you are not)

like image 53
kamjagin Avatar answered Sep 21 '22 00:09

kamjagin


Please take a look here

HH is hour in a day (starting from 0 to 23)

hh are hours in am/pm format

kk is hour in day (starting from 1 to 24)

mm is minute in hour

ss are the seconds in a minute

like image 36
peshkira Avatar answered Sep 21 '22 00:09

peshkira