How to format a Date in AM/PM in Java

How to format a Date in AM/PM in Java:

In this post, we will learn how to format a Date to AM/PM in Java. The SimpleDateFormat class makes it easy to format a given date to a specific pattern. Let’s learn how this class can be used to convert a date to a 12 Hour format with AM/PM.

SimpleDateFormat:

The SimpleDateFormat class is a useful class to parse a Date in Java. We can pass one pattern while creating one object of SimpleDateFormat and it will use this pattern to format the date.

We can use any of the following letters in the pattern:

Letter Description
G Era designator, e.g. AD
y year
Y Week year
M Month of a year
w Week of a year
W Week of a month
D Day of a year
d Day of a month
F Day of a week for a month
E Day name of a week
u Day number of a week, 1 is for Monday…7 is for Sunday
a Am/pm
H hour in a day, it is in between 0 to 23
k hour in a day in between 1 to 24
K hour in am/pm, in between 0 to 11
h hour in am/pm, in between 1 to 12
m minute in hour
s second in minute
S millisecond
z Time zone
Z Time zone
X Time zone

Let me show you how it works with an example:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        Date currentDate = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = formatter.format(currentDate);

        System.out.println(formattedDate);
    }
}
  • The currentDate object is the current Date object.
  • We created one SimpleDateFormat object with pattern ‘dd-MM-yyyy’. It will format the date with day, month and year.
  • The format method formats the date object and returns the result as a string.

If you run this program, it will print:

08-03-2023

Example to format a date in AM/PM:

Let’s use SimpleDateFormat class to display the time in AM/PM format. We can use the following patterns to format the date:

  • hh to display the time in 12 hours format
  • mm to display the minute
  • aa for AM/PM

The following program displays the current Date time in AM/PM format:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        Date currentDate = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm aa");
        String formattedDate = formatter.format(currentDate);

        System.out.println(formattedDate);
    }
}

We are using the hh:mm aa pattern. This will print output as below:

12:47 pm

We can also use the pattern “hh.mm aa” to print the date as 12.51 pm.

Example 2: Convert a 24 hour time to 12 hour with AM/PM:

Suppose a string is given in 24 hour format. We can use the SimpleDateFormat class to parse a string and convert it to a Date object. The parse() method of SimpleDateFormat is used to parse a string to Date.

Once the string is parsed, we can use another SimpleDateFormat object as shown in the previous example to convert it to a 12 hour AM/PM value.

Let me show you how to do that with one example:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        String time = "21.15";
        SimpleDateFormat format = new SimpleDateFormat("HH.mm");

        try {
            Date date = format.parse(time);
            SimpleDateFormat formatter = new SimpleDateFormat("hh.mm aa");
            String formattedDate = formatter.format(date);

            System.out.println(formattedDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
    }
}
  • The given time is 21.15. It is in 24 hours format and it is a string.
  • We created one SimpleDateFormat object and used the parse() method to convert this value to a Date object.
  • Once the date is created, we used another SimpleDateFormat object to convert it to a 12 hour AM/PM value.
  • The final output is assigned to the string formattedDate and its value is printed.

It will print:

09.15 pm

Note that we have to use a try-catch block as the parse method might throw an ParseException. If any exception is thrown, it will print the stacktrace.

You might also like: