How to Format Time in AM PM with Java


This example we will show you how to format given time into AM PM format. To format Time we will use SimpleDateFormat class. This SimpleDateFormat class will has construcotr to pass the time format. SimpleDateFormat("HH-mm-ss a").

package com.rrtutors;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test{
    public static void main(String[] args){
        Date date = new Date();
        String strDateFormat = "HH:mm:ss a";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
}

 

Output:

16:03:23 PM

JAVA File IO examples

JAVA Date & Time Progamming Examples