Java tutorial
//package com.java2s; //License from project: Open Source License import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static String FormatTime(String Time, String timeformat) { DateFormat formatter; Date convertedDate = null; formatter = new SimpleDateFormat("HH:mm"); try { convertedDate = (Date) formatter.parse(Time); } catch (ParseException e) { //e.printStackTrace(); } if (timeformat.equals("24")) return formatter.format(convertedDate); else return ampmChanger(formatter.format(convertedDate)); } public static String ampmChanger(String time) { int hour = Integer.parseInt(time.substring(0, 2)); String timeAppendage; if (hour > 12) { // For 1 PM and on hour -= 12; timeAppendage = "p"; } else if (hour == 12) timeAppendage = "p"; // For 12 pm else timeAppendage = "a"; // For 0(12AM) to Noon return String.valueOf(hour) + time.substring(2) + timeAppendage; } }