In this tutorial we will see how to convert 12 hour format to 24 hour format and 24 hour format to 12 hour format in Java.
Java – Convert 12 Hour data time format to 24 hour date time format
We can change the pattern in the SimpleDateFormat for the conversion. The pattern dd/MM/yyyy hh:mm:ss aa is used for the 12 hour format and the pattern MM-dd-yyyy HH:mm:ss is used for the 24 hour format. In this program we are changing the format of the date by changing the patterns and formatting the input date.
Input/Output of the Program:
Input date and time: 23/12/2014 12:22:12 PM (12 hour format)
Output date and time: 12-23-2014 22:22:12 (24 hour format)
import java.text.SimpleDateFormat; import java.text.DateFormat; import java.util.Date; import java.text.ParseException; class DateTimeFormatDemo { public static void main(String[] args) { String input = "23/12/2014 10:22:12 PM"; //Format of the date defined in the input String DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa"); //Desired format: 24 hour format: Change the pattern as per the need DateFormat outputformat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"); Date date = null; String output = null; try{ //Converting the input String to Date date= df.parse(input); //Changing the format of date and storing it in String output = outputformat.format(date); //Displaying the date System.out.println(output); }catch(ParseException pe){ pe.printStackTrace(); } } }
Output:
12-23-2014 22:22:12
Java – Convert 24 Hour format to 12 hour format
In this example we are doing the opposite of what we have done in the above example. Here we are converting the 24 hour format of input date into a 12 hour format.
Input/Output of the program:
Input date and time: 15/02/2014 22:22:12(24 hour format dd/MM/yyyy HH:mm:ss
)
Output date and time: 2014-02-15 10:22:12 PM (12 hour format yyyy-MM-dd hh:mm:ss aa
)
import java.text.SimpleDateFormat; import java.text.DateFormat; import java.util.Date; import java.text.ParseException; class Example2 { public static void main(String[] args) { //Input date in String format String input = "15/02/2014 22:22:12"; //Date/time pattern of input date DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); //Date/time pattern of desired output date DateFormat outputformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa"); Date date = null; String output = null; try{ //Conversion of input String to date date= df.parse(input); //old date format to new date format output = outputformat.format(date); System.out.println(output); }catch(ParseException pe){ pe.printStackTrace(); } } }
Output:
2014-02-15 10:22:12 PM
Kushagra says
very thanks the most simple method i found. Can you tell why we have to use try-catch block.
Eric says
I accidentally landed in this page and found the comment. Thought I would give my reply since no one replied to the comment!
Answer for reason for try-catch block:
Since the input in the program is hardcoded, technically we wouldn’t need a try catch block here, however no program in any world would hardcode a value like this. And hence, the developer has thought in advance that the input will definitely be parameterized. That means, there is a margin for error which is why we have the try-catch block!! Hope I’m clear!!