Add AM PM to time format using SimpleDateFormat

 

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    // formatting time to have AM/PM text using 'a' format
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
    System.out.println("Time with AM/PM field: " + sdf.format(date));
  }
}

 

The output:


Time with AM/PM field: 09:24:13 AM
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.