In the previous article, we have seen Java Program to Convert Centimeter to Feet and Inches
In this article we are going to see how convert time from 12-hour to 24-hour format using Java programming language.
Java Program to Convert Time from 12-Hour to 24-Hour Format
It is considered that the time format is given in hh:mm:ss format.
Let’s see different ways to convert time from 12-hour to 24-hour format.
Method-1: Java Program to Convert Time from 12-Hour to 24-Hour Format By Using Static Input Value
Approach:
- Initialize time value.
- Call the method for conversion.
- Check if the time is in AM or PM.
- If it’s in AM:
- Check if the hh value is 12 print “00”and print the minutes and seconds as it is.
- Else if it isn’t 12, print all the values as it is.
- Else if it’s in PM.
- If the hh value is 12, print all the values as it is.
- Else add 12 to the hh value and print it, and print the rest values as it is.
Program:
public class Main { static void print24(String str) { // get hours int h1 = (int) str.charAt(1) - '0'; int h2 = (int) str.charAt(0) - '0'; int hh = (h2 * 10 + h1 % 10); // if time is in "AM" if (str.charAt(8) == 'A') { if (hh == 12) { System.out.print("00"); for (int i = 2; i <= 7; i++) System.out.print(str.charAt(i)); } else { for (int i = 0; i <= 7; i++) System.out.print(str.charAt(i)); } } // if time is in "PM" else { if (hh == 12) { System.out.print("12"); for (int i = 2; i <= 7; i++) System.out.print(str.charAt(i)); } else { hh = hh + 12; System.out.print(hh); for (int i = 2; i <= 7; i++) System.out.print(str.charAt(i)); } } } public static void main(String[] args) { String str = "04:13:34PM"; System.out.print("Time in 24hrs is "); print24(str); } }
Output: Time in 24hrs is 16:13:34
Method-2: Java Program to Convert Time from 12-Hour to 24-Hour Format By Using User Input Value
Approach:
- Create Scanner class object.
- Take user input for hour, minute and seconds and whether it’s am or pm.
- If the user has given single value in input pad a zero to the left.
- Convert the AM/PM value to upper case(for easier comparison).
- Call the method for conversion.
- Check if the time is in AM or PM.
- If it’s in AM:
- Check if the hh value is 12 print “00”and print the minutes and seconds as it is.
- Else if it isn’t 12, print all the values as it is.
- Else if it’s in PM.
- If the hh value is 12, print all the values as it is.
- Else add 12 to the hh value and print it, and print the rest values as it is.
Program:
import java.util.Scanner; public class Main { static void print24(String str) { // get hours int h1 = (int) str.charAt(1) - '0'; int h2 = (int) str.charAt(0) - '0'; int hh = (h2 * 10 + h1 % 10); // if time is in "AM" if (str.charAt(8) == 'A') { if (hh == 12) { System.out.print("00"); for (int i = 2; i <= 7; i++) System.out.print(str.charAt(i)); } else { for (int i = 0; i <= 7; i++) System.out.print(str.charAt(i)); } } // if time is in "PM" else { if (hh == 12) { System.out.print("12"); for (int i = 2; i <= 7; i++) System.out.print(str.charAt(i)); } else { hh = hh + 12; System.out.print(hh); for (int i = 2; i <= 7; i++) System.out.print(str.charAt(i)); } } } //driver method public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the time in 24 hour format"); System.out.println("Enter hours: "); String hh = sc.nextLine(); hh = padZero(hh); System.out.println("Enter minutes: "); String mm = sc.nextLine(); mm = padZero(mm); System.out.println("Enter seconds: "); String ss = sc.nextLine(); ss = padZero(ss); System.out.println("Enter AM or PM: "); String ampm = sc.nextLine(); String time = hh + ":" + mm + ":" + ss + ampm.toUpperCase(); System.out.print("Time in 24-hours format is "); print24(time); } private static String padZero(String n) { if (n.length() == 1) n = "0" + n; return n; } }
Output: Enter the time in 24 hour format Enter hours: 2 Enter minutes: 3 Enter seconds: 4 Enter AM or PM: pm Time in 24-hours format is 14:03:04
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: