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
[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.
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.
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.
DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With