Add AM PM to time using SimpleDateFormat - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

Add AM PM to time using SimpleDateFormat

Demo Code


 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
 
  public static void main(String[] args) {
    Date date = new Date();
   /*from   ww w . j av a 2  s  . c  om*/
    //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));
 
  }
}

Result


Related Tutorials