- 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 display hour and minute in AM or PM
Firstly, create a Formatter and a Calendar object.
Formatter f = new Formatter(); Calendar c = Calendar.getInstance();
For AM/ PM marker, use the following conversion character −
p
Here, you can see AM/ PM marker −
f = new Formatter(); System.out.println(f.format("Hour: %tl %1$Tp", c));
The following is an example −
Example
import java.util.Calendar; import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar c = Calendar.getInstance(); System.out.println("Current date and time: "+c.getTime()); f = new Formatter(); System.out.println(f.format("Hour and Minute with AM/ PM: %tl:%1$tM %1$Tp", c)); f = new Formatter(); System.out.println(f.format("Hour: %tl %1$Tp", c)); f = new Formatter(); System.out.println(f.format("Minute: %1$tM", c)); } }
Output
Current date and time: Mon Nov 26 07:41:35 UTC 2018 Hour and Minute with AM/ PM: 7:41 AM Hour: 7 AM Minute: 41
- Related Articles
- 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
- Display just hour and minute using Formatter in Java
- Java Program to Format time in AM-PM format
- Display AM/PM time marker with SimpleDateFormat(“a”) in Java
- How do you display JavaScript datetime in 12hour AM/PM format?
- Java Program to display Time in 12-hour format
- Java Program to display time in 24-hour format
- Format MySQL CURRENT_TIMESTAMP to AM & PM?
- What is the smaller angle between the minute and hour hands at 12:46 AM? Â
- How to sort time in AM/ PM in MySQL?
- Program to find angle between hour and minute hands of a clock in C++?
- MySQL format time with lowercase am/pm?
- Display hour with SimpleDateFormat(“H”) in Java

Advertisements