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
22:23 military time Military time uses a very unique two-digit number to identify each of the twenty-four hours in a day, eliminating the need for a.m. and p.m. To clearly distinguish the time of day, regular time necessitates the usage of a.m. and p.m.
ReplyDelete22:23 military time Military time uses a very unique two-digit number to identify each of the twenty-four hours in a day, eliminating the need for a.m. and p.m. To clearly distinguish the time of day, regular time necessitates the usage of a.m. and p.m.
ReplyDelete