Java Date – Convert 12 hour format to 24 hour format and vice versa

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.

Comments

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!!