- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Format time in AM-PM format
In this article, we will understand how to format time in AM-PM format. A formatting string describes how a date/time values should be read and written from(to) string representation (flat files, human readable output, etc.)
Below is a demonstration of the same −
Suppose our input is −
Current date: Thu Mar 17 16:04:31 IST 2022
The desired output would be −
The current Time in AM/PM format is : 04.04 pm
Algorithm
Step 1 - START Step 2 - Declare a date object namely current_date that fetches the current date and time. Step 3 - Define the values. Step 4 - Declare an object ‘formatTime’ of class SimpleDateFormat. Step 5 - Use the function .format(current_date) to format the time to the specified format. Step 6 - Display the result Step 7 - Stop
Example 1
Here, the input is being entered by the user based on a prompt.
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); } }
Output
The required packages have been imported The current date is: Thu Mar 17 16:04:31 IST 2022 The current Time in AM/PM format is : 04.04 pm
Example 2
Here, we have defined a function to compute the standard deviation.
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); } }
Output
The required packages have been imported The current date is: Thu Mar 17 16:04:31 IST 2022 The current Time in AM/PM format is : 04.04 pm
- Related Articles
- MySQL format time with lowercase am/pm?
- Format MySQL CURRENT_TIMESTAMP to AM & PM?
- Java Program to display hour in K (0-11 in AM/PM) format
- Display hour in h (1-12 in AM/PM) format in Java
- Display hour in hh (01-12 in AM/PM) format in Java
- Java Program to format time using Custom Format
- How do you display JavaScript datetime in 12hour AM/PM format?
- Java Program to format time with DateFormat.MEDIUM
- Java Program to format time with DateFormat.LONG
- Java Program to format date as Apr 19, 2019, 1:27 PM
- Convert PHP variable “11:00 AM” to MySQL time format?
- Java Program to format date time with Join
- Java Program to parse Time using custom format
- Java Program to display Time in 12-hour format
- Java Program to display time in 24-hour format

Advertisements