Java Program to Convert Time from 24-Hour to 12-Hour Format

In the previous article, we have seen Java Program to Covert Time From 12-hour Format to 24-hour Format

In this article we are going to see how convert time from 24-hour to 12-hour format using Java programming language.

Java Program to Convert Time from 24-Hour to 12-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 24-hour to 12-hour format.

Method-1: Java Program to Convert Time from 24-Hour to 12-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 by comparing the hh value with 12.
  • Do hh = hh%12.
  • If hh == 0 i.e., AM:
    1. Print 12, and print the minutes and seconds as it is.
  • Else if it’s in PM.
    1. If the hh as it is.
    2. Print the rest values as it is.

Program:

public class Main
{
    static void convert(String str) 
    {
        // Get Hours
        int h1 = (int) str.charAt(0) - '0';
        int h2 = (int) str.charAt(1) - '0';

        int hh = h1 * 10 + h2;

        // check if the time should be in AM or PM
        String mrdn;
        if (hh < 12)
        {
            mrdn = "AM";
        } else
            mrdn = "PM";

        hh %= 12;

        // Handle 00 and 12 case separately
        if (hh == 0)
        {
            System.out.print("12");

            // Printing minutes and seconds
            for (int i = 2; i < 8; ++i) 
            {
                System.out.print(str.charAt(i));
            }
        }
        else
        {
            System.out.print(hh);
            // Printing minutes and seconds
            for (int i = 2; i < 8; ++i) {
                System.out.print(str.charAt(i));
            }
        }

        // After time is printed print Meridien
        System.out.println(" " + mrdn);
    }

    public static void main(String ar[])
    {

        // 24 hour format
        String str = "13:58:28";
        convert(str);

    }
}
Output:

1:58:28 PM

Method-2: Java Program to Convert Time from 24-Hour to 12-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 by comparing the hh value with 12.
  • Do hh = hh%12.
  • If hh == 0 i.e., AM:
    1. Print 12, and print the minutes and seconds as it is.
  • Else if it’s in PM.
    1. If the hh as it is.
    2. Print the rest values as it is.

Program:

import java.util.Scanner;

public class Main 
{
    static void convert(String str) 
    {
        // Get Hours
        int h1 = (int) str.charAt(0) - '0';
        int h2 = (int) str.charAt(1) - '0';

        int hh = h1 * 10 + h2;

        // check if the time should be in AM or PM
        String mrdn;
        if (hh < 12) 
        {
            mrdn = "AM";
        } else
            mrdn = "PM";

        hh %= 12;

        // Handle 00 and 12 case separately
        if (hh == 0)
        {
            System.out.print("12");

            // Printing minutes and seconds
            for (int i = 2; i < 8; ++i)
            {
                System.out.print(str.charAt(i));
            }
        } 
        else 
        {
            System.out.print(hh);
            // Printing minutes and seconds
            for (int i = 2; i < 8; ++i) 
            {
                System.out.print(str.charAt(i));
            }
        }

        // After time is printed print Meridien
        System.out.println(" " + mrdn);
    }

    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);

        String time = hh + ":" + mm + ":" + ss;
        System.out.print("Time in 12-hour format is ");
        convert(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: 
14
Enter minutes: 
32
Enter seconds: 
12
Time in 12-hour format is 2:32:12 PM

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Programs: