using the %t specifier with Formatter.
import java.util.Calendar; import java.util.Formatter; public class Main { public static void main(String[] argv) throws Exception { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); // Display 12-hour time format. fmt.format("Time using 12-hour clock: %tr\n", cal); System.out.println(fmt); } } //Time using 12-hour clock: 03:00:51 PM