Format Time in AM/PM Format in Java



Time formatting is a process of converting date and time values into human-readable strings with specific formats. The resulting string describes how date/time values should be represented (such as flat files or human-readable output). In this article, we will understand how to format time in AM-PM format.

In 12-hour clock system, AM-PM format is used to define time. Here, AM stands for Ante meridiem which refers to the time before noon, whereas PM stands for Post meridiem which shows the time period after noon.

Example Scenario

Input: current_date = Thu Mar 17 16:04:31 IST 2024
Output: Current Time in AM/PM format is = 04.04 pm

Ways to Format time in AM-PM format

To format time in AM-PM format, we can use the following approaches ?

  • Using SimpleDateFormat class
  • Using user-defined function

Using SimpleDateFormat Class

In Java, the SimpleDateFormat class is a subclass of DateFormat class. It provides multiple methods to parse and format date/time. It allows us to choose any user-defined pattern for formatting purpose.

Example

In this example, we will use the format() method of the SimpleDateFormat class to format time in AM-PM.

import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      Date current_date = new Date();
      System.out.println("The current date is: " + current_date);
      SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa");
      String result_time = formatTime.format(current_date);
      System.out.println("\nThe current Time in AM/PM format is : " + result_time);
   }
}

On running the above code, it will display the below output ?

The required packages have been imported
The current date is: Thu Mar 17 16:04:31 IST 2024

The current Time in AM/PM format is : 04.04 pm

Using user-defined function

In a user-defined function, we will pass the current date and use the format() method to format date and time values in AM-PM format. A block of code that is created by a programmer to perform a specific task is called as user-defined function.

Example

Here, we have defined a function to format date/time in AM-PM format.

import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo {
   static void format_time(Date current_date){
      SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa");
      String result_time = formatTime.format( current_date);
      System.out.println("\nThe current Time in AM/PM format is : " + result_time);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      Date current_date = new Date();
      System.out.println("The current date is: " + current_date);
      format_time(current_date);
   }
}

On executing, this code will produce the following result ?

The required packages have been imported
The current date is: Thu Mar 17 16:04:31 IST 2024
The current Time in AM/PM format is : 04.04 pm
Updated on: 2024-08-01T10:59:17+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements