Java Date Format AM PM
Chapter:
Date and Time
Last Updated:
14-05-2023 11:49:13 UTC
Program:
/* ............... START ............... */
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
String formattedDate = sdf.format(now);
System.out.println(formattedDate);
}
}
/* ............... END ............... */
Output
14-05-2023 09:30:15 AM
Please note that the actual output will depend on the current date and time when you run the program.
Notes:
-
In this example, the pattern "dd-MM-yyyy hh:mm:ss a" is used to format the date. The "a" represents the AM/PM marker. When you run this code, it will display the current date and time in the specified format, including the AM/PM indicator.
- Note that the "hh" pattern is used for a 12-hour clock format, while "HH" is used for a 24-hour clock format.