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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With