Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from 12 hours time to 24 hours time in java

Tags:

java

time

In my app, I have a requirement to format 12 hours time to 24 hours time. What is the method I have to use?

For example, time like 10:30 AM. How can I convert to 24 hours time in java?

like image 661
koti Avatar asked Jun 30 '11 07:06

koti


People also ask

How do you convert 12 hours to 24 hours?

For a military time that is larger than 12:00, just subtract 12 hours to get the 24 hour(standard time), then add “pm”. For example, if you have 14:30 hours, subtract 12 hours and the result is 2:30 pm.

How do I convert 12 hour format to 24 hour format in SQL?

In SQL Server 2012, we can use Format function to have suitable date time format. Use capital letter 'HH:mm:ss' for 24 hour date time format.

How do you convert time in Java?

mport java. util. Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int days=24; int hours = 60; int mins=60; int res=days*hours*mins; System.


1 Answers

Try this:

import java.text.SimpleDateFormat; import java.util.Date;  public class Main {    public static void main(String [] args) throws Exception {        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));    } } 

which produces:

10:30 PM = 22:30 

See: http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

like image 197
Bart Kiers Avatar answered Sep 20 '22 10:09

Bart Kiers