Today we are going to see, how we can convert a 12 hour time format into 24 hour(military) time format.
For example we have date like 12 Aug, 2018 07:05 PM or 04/03/1965 02:06:45 AM or just only have time 07:15:00 AM or 08:50:25 PM which we want to convert in 24 hours format and remove the AM and PM from the date.
For example we have date like 12 Aug, 2018 07:05 PM or 04/03/1965 02:06:45 AM or just only have time 07:15:00 AM or 08:50:25 PM which we want to convert in 24 hours format and remove the AM and PM from the date.
Here I have written a simple program which help us to understand the process and what steps do we need to perform to convert a string time into 24 hours of time format.
Below are the code snippet.
Below are the code snippet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| package jorvee;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class timeFormet {
public static void main(String[] args) throws ParseException {
String time="07:05:45 PM";
/*
* This dateFormat will help you to first convert your string time into DateTime format.
*/
DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss aa");
Date date24=dateFormat.parse(time);
/*
* Convert your time into 24 hours date format
*/
DateFormat outFormat = new SimpleDateFormat( "HH:mm:ss");
System.out.println(outFormat.format(date24));
}
}
|
Output:
19:05:45