1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* Add AM/PM to time using SimpleDateFormat This example shows how to format time to have a AM/PM text using Java SimpleDateFormat class. */ import java.text.SimpleDateFormat; import java.util.Date; public class AddAMPMToFormattedDate { public static void main(String[] args) { //create Date object Date date = new Date(); //formatting time to have AM/PM text using 'a' format String strDateFormat = "HH:mm:ss a"; SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); System.out.println("Time with AM/PM field : " + sdf.format(date)); } } /* Typical output would be Time with AM/PM field : 01:39:42 AM */ |
you used HH, what if it was in the afternoon at 1pm? the result would be
13:00:00 PM
you shouldve used K or h
I want to display the AM/PM past as single character as a/p.
In the given example the time is 01:39:42 AM but I want to show it as 01:39:42 A.
I tried both HH:mm:ss a and HH:mm:ss aaa but the results are same.
just try “hh:mm:ss a ” instead of “HH:mm:ss a “