Thursday, January 18, 2024

Display Time in 24 Hours Format in Java

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

  1. How to Format Time in AM-PM Format - Java Program
  2. Create Date Object in Java With Date and Time Values
  3. Java Program to Get Current Date and Time
  4. How to Convert Date And Time Between Different Time-Zones in Java
  5. Difference Between Two Dates in Java

You may also like-

  1. Ternary Search Program in Java
  2. Count Number of Times Each Character Appears in a String Java Program
  3. How to Convert a File to Byte Array
  4. Setting And Getting Thread Name And Thread ID in Java
  5. Java Lambda Expression as Method Parameter
  6. Interface Default Methods in Java
  7. Service in Angular With Examples
  8. Spring NamedParameterJdbcTemplate Insert, Update And Delete Example