In this article we’ll see a Java program to display time in 24 hour format. The pattern specified in SimpleDateFormat or DateFormatter class
(Java 8 onward) for time determines whether time is shown in 12 hour format or 24 hour format. For 24 hour format you will have to use HH for
hour not hh.
Time in 24 hour format using DateFormatter
LocalTime time = LocalTime.now(); // Pattern for 24 hrs DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm:ss"); System.out.println("Time (24 Hour format) - " + time.format(pattern));
Output
Time (24 Hour format) – 16:43:45
Time in 24 hour format using SimpleDateFormat
Date date = new Date(); // Pattern for 24 Hrs. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); System.out.println("Time (24 Hour format) - " + sdf.format(date));
Output
Time (24 Hour format) – 16:45:30
That's all for this topic Display Time in 24 Hours Format in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
You may also like-