java convert 12 hours to 24 hours format

java convert 12 hours to 24 hours format
‮efer‬r to:lautturi.com
/**
 * @author lautturi.com
 * Java example:java convert AM/PM to 24 hours format
 */

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

public class Lautturi {

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
		SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
		Date date = parseFormat.parse("10:30 PM");
		System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));

	}
}

output:

10:30 pm = 22:30

If your date and time formats can not be parsed, It will throw an exception :

Exception in thread "main" java.text.ParseException: Unparseable date: "10:30 PM"
	at java.base/java.text.DateFormat.parse(DateFormat.java:395)

Open Control panel. ->

Control Panel\Clock, Language, and Region -> Change date, time, or number formats

Created Time:2017-09-21 12:58:29  Author:lautturi