- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 display Time in 12-hour format
Use the SimpleDateFormat class to display time in 12-hour format.
Set the format
Date dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("hh:mm:ss a");
Now, the following will display time in 12-hour format
dateFormat.format(dt)
The following is an example
Example
import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] argv) throws Exception { Date dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("hh:mm:ss a"); System.out.println("Time in 12 hr format = "+dateFormat.format(dt)); } }
Output
Time in 12 hr format = 11:33:53 AM
- Related Articles
- Java Program to display time in 24-hour format
- Converting 12 hour format time to 24 hour format in JavaScript
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- C# program to convert time from 12 hour to 24 hour 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
- Convert time from 24 hour clock to 12 hour clock format in C++
- Java Program to Display time in different country’s format
- Java Program to display hour in K (0-11 in AM/PM) format
- How to Convert Time Format from 12 Hour to 24 Hour and Vice Versa in Excel?
- Java Program to format hour in H (0-23) format in Java
- Display hour in KK (00-11) format in Java
- Program to convert hour minutes’ time to text format in Python
- Python Pandas - Format the Period object and display the Time with 24-Hour format

Advertisements