Format Timestamp in 12 Hours Pattern (AM-PM) in Java

Learn to format date and time in either 12 hours pattern. The formatted date string will have the AM-PM information as applicable to the timestamp.

1. Patterns to Display Hours

For formatting purposes, hour part of the time is represented in two ways:

  • ‘hh’ – hours in 12 hour format
  • ‘HH’ – hours in 24 hour format
  • ‘a’ – display the AM/PM information.

Using the above information, we can create the following pattern to display time in 12-hour format including AM/PM information in formatted date string.

hh:mm:ss a

2. Demo

Java program to display the current date-time in 12-hour format. We will create examples for LocalTime and LocalDateTime classes.

For Date and Calendar classes, we can use SimpleDateFormat while for other classes we can use DateTimeFormatter.

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main
    public static void main(String[] args)
        String pattern = "hh:mm:ss a";
        //1. LocalTime
        LocalTime now =;

        //2. LocalDateTime
        LocalDateTime nowTime =;

Program output.

07:35:55 PM
07:35:55 PM

Happy Learning !!


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode