BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – Display time in 12 hour format with AM/PM using SimpleDateFormat

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will see how to display time in 12 hour format with AM/PM using the SimpleDateFormat.

1. Display current date and time in 12 hour format with AM/PM

There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM.
hh – hours in 12 hour format
mm – minutes
aa – AM/PM marker.
In this example we are displaying current date and time with AM/PM marker.

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
class Example
{
    public static void main(String[] args)
    {	
    	//Displaying current time in 12 hour format with AM/PM
    	DateFormat dateFormat = new SimpleDateFormat("hh.mm aa");
    	String dateString = dateFormat.format(new Date()).toString();
    	System.out.println("Current time in AM/PM: "+dateString);
    	
    	//Displaying current date and time in 12 hour format with AM/PM
    	DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy hh.mm aa");
    	String dateString2 = dateFormat2.format(new Date()).toString();
    	System.out.println("Current date and time in AM/PM: "+dateString2);
    }
}

Output:

Current time in AM/PM: 12.30 PM
Current date and time in AM/PM: 20/10/2017 12.30 PM

2. Displaying given date and time with AM/PM

In the above example, we are showing the current date and time with AM/PM. In this example we are displaying the given date and time with AM/PM.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
class Example
{
    public static void main(String[] args)
    {	
    	
    	//Displaying given time in 12 hour format with AM/PM
    	String dateString3 = "22.30"; 
    	//old format
    	SimpleDateFormat sdf = new SimpleDateFormat("HH.mm");
    	try{
    	Date date3 = sdf.parse(dateString3);
    	//new format
    	SimpleDateFormat sdf2 = new SimpleDateFormat("hh.mm aa");
    	//formatting the given time to new format with AM/PM
    	System.out.println("Given time in AM/PM: "+sdf2.format(date3));
    	}catch(ParseException e){
    		e.printStackTrace();
    	}
    	
    	//Displaying given time in 12 hour format with AM/PM
    	String dateString4 = "21/12/2016 21.20"; 
    	//old format
    	SimpleDateFormat sdf3 = new SimpleDateFormat("dd/MM/yyyy HH.mm");
    	try{
    	Date date4 = sdf3.parse(dateString4);
    	//new format
    	SimpleDateFormat sdf4 = new SimpleDateFormat("dd/MM/yyyy hh.mm aa");
    	//formatting the given time to new format with AM/PM
    	System.out.println("Given date and time in AM/PM: "+sdf4.format(date4));
    	}catch(ParseException e){
    		e.printStackTrace();
    	}
    }
}

Output:

Given time in AM/PM: 10.30 PM
Given date and time in AM/PM: 21/12/2016 09.20 PM

Related Posts:

  1. Java – Convert 12 hour format to 24 hour format and vice versa
  2. Java – Parse the date in desired format
  3. Java – SimpleDateFormat class
  4. Java – Convert String to 24 hour format
  5. Java 8 – display current date and time

Top Related Articles:

  1. Convert String to date in Java
  2. Date validation in java
  3. Compare two dates with each other in Java
  4. Java 8 – Calculate days between two dates
  5. How to convert String to 24 hour date time format in java

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap