In the previous article, we have discussed about Java Program to Display Current Time
In this article we are going to see how to display current time in AM/PM using the SimpleDateFormat in Java along with suitable examples.
Java Program to Display Current Time in AM/PM Format
Explanation:
The java.text.SimpleDateFormat
class provides inbuild methods to format the date and time in java.
There are 2 patterns which we can use in SimpleDateFormat to display the time.
- “hh:mm:ss aa” , here hh is used for 12 hour format with AM/PM.
- “HH:mm:ss aa”, here HH is used for 24 hour format without AM/PM.
Where,
- hh: hours in 12 hour format
- HH: hours in 24 hour format
- mm: minutes
- ss: Seconds
- aa: AM/PM marker
Let’s see the program to understand it more clearly.
- Java Program to Display Current Time in AM/PM Format by Using hh:mm aa Pattern
- Java Program to Display Current Time in AM/PM Format by Using HH:mm:ss aa Pattern
Method-1: Java Program to Display Current Time in AM/PM Format by Using hh:mm aa Pattern
Approach:
- Create an object of
Date
class which takes the system date and time. - Declare a string variable as ‘
dateFormat
’ and initialize it to “hh:mm:ss aa
” pattern - Create an object of
SimpleDateFormat
as ‘s
’ with the argument as ‘dateFormat
’. - Print the result using method of
SimpleDateFormat
class as ‘s.format(date)
’
Program:
import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { // Create an object of Date class which takes the system date and time Date date = new Date(); // Declare a string variable as ‘dateFormat’ and initialize it to "hh:mm:ss aa" pattern String dateFormat = "hh:mm:ss aa"; // Create an object of SimpleDateFormat as ‘s’ with the argument as ‘dateFormat’. SimpleDateFormat s = new SimpleDateFormat(dateFormat); //Print the result using method of SimpleDateFormat class as ‘s.format(date)’ System.out.println("The current time in 12 hours format is: "+s.format(date)); } }
Output: The current time in 12 hours format is: 06:23:50 AM
Method-2: Java Program to Display Current Time in AM/PM Format by Using HH:mm:ss aa Pattern
Approach:
- Create an object of
Date
class which takes the system date and time - Declare a string variable as ‘
dateFormat
’ and initialize it to “HH:mm:ss aa
” pattern - Create an object of
SimpleDateFormat
as ‘s
’ with the argument as ‘dateFormat
’. - Print the result using method of
SimpleDateFormat
class as ‘s.format(date)
’
Program:
import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { // Create an object of Date class which takes the system date and time Date date = new Date(); // Declare a string variable as ‘dateFormat’ and initialize it to "hh:mm:ss aa" pattern String dateFormat = "HH:mm:ss aa"; // Create an object of SimpleDateFormat as ‘s’ with the argument as ‘dateFormat’. SimpleDateFormat s = new SimpleDateFormat(dateFormat); //Print the result using method of SimpleDateFormat class as ‘s.format(date)’ System.out.println("The current time in 24 hours format is: "+s.format(date)); } }
Output: The current time in 24 hours format is: 15:35:56 PM
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: