How to Display Time in AM-PM Format in Java

Daftar Isi: (toc)

In this post we'll see how to display time in 12 hours format with AM-PM in Java.

In order to display time with AM-PM in Java, in the pattern you are creating for the format either with SimpleDateFormat (if you are not using Java 8) or with DateFormatter (if you are using Java 8) just add the pattern letter ‘a’ which denotes AM-PM of the day.

Displaying time in AM-PM format - Java SimpleDateFormat Example

If you are using the java.util.Date and SimpleDateFormat then the format you need to use to show time in AM-PM format is as following.

Date date = new Date();
// Pattern
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
System.out.println("TIME - " + sdf.format(date));


TIME - 13:09:55 PM

Displaying time with AM-PM format - Java DateFormatter Example

If you are using the new Date and Time API in Java 8, then you can use the DateFormatter class, pattern remains the same as used above for showing time in AM-PM format in Java.

//Getting time
LocalTime t2 =;
// Pattern
DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss a");
String text = t2.format(df);
System.out.println("Time - " + text);


Time - 13:11:15 PM

Another example – Showing AM

LocalTime t1 = LocalTime.of(5, 30, 56);
DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss a");
String text = t1.format(df);
System.out.println("Time - " + text);


Time - 05:30:56 AM

That's all for this topic How to Display Time in AM-PM 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 date in Java using SimpleDateFormat
  2. How to convert String to Date in Java
  3. How to convert date and time between different time-zones in Java
  4. How to find last modified date of a file in Java
  5. Converting int to string - Java Program

You may also like-

  1. Connection Pooling Using Apache DBCP in Java
  2. Producer-Consumer Java Program Using ArrayBlockingQueue
  3. Converting String to Byte Array - Java Program
  4. Object Cloning in Java
  5. Nested class and Inner class in Java
  6. Reflection in Java
  7. How to resolve Local variable defined in an enclosing scope must be final or effectively final error
  8. Insert\Update using NamedParameterJDBCTemplate in Spring framework
Find Out
Related Post

Ikuti pada Aplikasi GOOGLE NEWS : FOLLOW (Dapatkan Berita Terupdate tentang Dunia Pendidikan dan Hiburan). Klik tanda  (bintang) pada aplikasi GOOGLE NEWS.

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad