Java Program for Conversion Between 12-Hour and 24-Hour Time Format

24hour-12hour

Java programs for conversion between 12-hour and 24-hour time format have been shown here. The 12-hour time format and the 24-hour time format are two different ways of representing time.


The 12-hour time format is a system in which the day is divided into two 12-hour periods: AM / am (ante meridiem) and PM / pm (post meridiem). In ante meridiem, the hours from 1 to 12 are used to represent the time from midnight to noon, while in post meridiem the hours from 1 to 12 are used to represent the time from noon to midnight. On the other hand, the 24-hour time format is a system in which the day is divided into 24 hours. In this system, the hours from 0 to 23 are used to represent the time from midnight to midnight.


For example, 09:30 am in the 12-hour format is equivalent to 09:30 in the 24-hour format, while 09:30 pm in the 12-hour format is equivalent to 21:30 in the 24-hour format.






1. Program & Output for Conversion Between 12-Hour Time Format and 24-Hour Format




1.1. Java Program & Output to convert 12 hour time format to 24 hour format

Code has been copied
/*******************************
       alphabetacoder.com
 Java program to convert 12 hour 
 time format to 24 hour format
********************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int h, m, h1, m1;
        String time, am_pm;

        // take inputs
        System.out.print("Enter the time in 12-hour format (hh:mm am/pm): ");
        time = sc.nextLine();

        // Split the input into hours, minutes, and am/pm parts
        String[] p = time.split(":");
        // get hour
        h = Integer.parseInt(p[0]);
        // get minute
        m = Integer.parseInt((p[1].split(" "))[0]);
        // get am or pm
        am_pm = (p[1].split(" "))[1];

        // copy value
        h1 = h;
        m1 = m;

        // Adjust the hour
        if (am_pm.equals("am") || am_pm.equals("AM")) { // if it is am / AM
            // if hour = 12, then it is 0 in 24 hour format
            if (h1 == 12) {
                h1 = 0;
            }
            // display result
            System.out.println(h + ":" + m + " a.m is same as " + String.format("%02d", h1) + ":" + String.format("%02d", m1) + " in 24-hour format");
        } else if (am_pm.equals("pm") || am_pm.equals("PM")) { // if it is pm / PM
            // if hour not equals 12, then add 12 in 24 hour format    
            if (h1 != 12) {
                h1 += 12;
            }
            // display result
            System.out.println(h + ":" + m + " p.m is same as " + String.format("%02d", h1) + ":" + String.format("%02d", m1) + " in 24-hour format");
        }
    }
}

Output


Enter the time in 12-hour format (hh:mm am/pm): 2:45 am

2:45 a.m is same as 02:45 in 24-hour format





1.2. Java Program & Output to convert 24 hour time format to 12 hour format

Code has been copied
/********************************
       alphabetacoder.com
 Java program to convert 24 hour 
 time format to 12 hour format
*********************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int h, m, h1, m1;
        String time;

        // take inputs
        System.out.print("Enter the time in 24-hour format (hh:mm): ");
        time = sc.nextLine();

        // Split the input into hours, minutes
        String[] p = time.split(":");
        // get hour
        h = Integer.parseInt(p[0]);
        // get minute
        m = Integer.parseInt(p[1]);

        // copy value
        h1 = h;
        m1 = m;

        // Adjust the hour
        if (h1 < 12) { // if hour value less than 12
            // display the result
            System.out.println(h + ":" + m + " is same as " + String.format("%02d", h1) + ":" + String.format("%02d", m1) + " a.m in 12-hour format");
        } else { // if hour value greater or equal to 12
            // subtract 12 from hour value
            h1 = h1 - 12;
            System.out.println(h + ":" + m + " is same as " + String.format("%02d", h1) + ":" + String.format("%02d", m1) + " p.m in 12-hour format");
        }
    }
}

Output


Enter the time in 24-hour format (hh:mm): 20:15

20:15 is same as 08:15 p.m in 12-hour format