Ticker

6/recent/ticker-posts

JAVA Program to Display Time in Words

 Question: Write a program that accepts time in the format "hours:minutes" in String Format. Accepts hours between 1 to 12 (both inclusive) and seconds between 0 to 59 (both inclusive) and prints out the time they represent in words.

In this post, I will share the JAVA program to display time in words.

Sample Input and Output:

Input 1:

Enter Time-> 3:0

Output 1:

Three O' Clock

Input 2:

Enter Time-> 7:29

Output 2:

Twenty Nine minutes past Seven

Input 3:

Enter Time-> 6:34
Output 3:

Twenty Six minutes to Seven

Input 4:

Enter Time-> 12:1
Output 4:

One minutes past Twelve

Input 5:

Enter Time-> 12:45

Output 5:

Quarter to One

Code:

import java.io.*;

public class English_Time {
    public static void main(String args[]) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String t = "Twenty ";// storing String data type
        /** String a[] contains numbers in words */
        String a[] = { """One""Two""Three""Four""Five""Six""Seven""Eight""Nine""ten""Eleven",
                "Twelve""Thirteen""Fourteen""Fifteen""Sixteen""Seventeen""Eighteen""Nineteen", t,
                t + "One", t + "Two", t + "Three", t + "Four", t + "Five", t + "Six", t + "Seven", t + "Eight",
                t + "Nine" };

        /** String s accepts Time in format HH:MM */
        System.out.print("Enter Time-> ");
        String s = in.readLine();

        /** int h and int m stores hours & minutes respectively from String s */
        int h = Integer.parseInt(s.substring(0s.indexOf(':')));
        int m = Integer.parseInt(s.substring(s.indexOf(':') + 1));

        /** if m is equal to 0 then the condition written below will run */
        if (m == 0)
            System.out.println(a[h] + " O' Clock");// displaying Time when m=0
        /** if m is equal to 30 then the condition written below will run */
        if (m == 30)
            System.out.println("Half past " + a[h]);// displaying Time when m=30

        /** if m is equal to 15 then the condition written below will run */
        if (m == 15)
            System.out.println("Quarter past " + a[h]);// displaying Time when m=15

        /** displaying Time when 0<m<30 but m does not equal to 15 */
        else if (m > 0 && m < 30)
            System.out.println(a[m] + " minutes past " + a[h]);

        /**
         * if h is equal to 12 then h=0 because after twelve o’clock, One o’clock comes
         */
        if (h == 12)
            h = 0;

        /** if m is equal to 45 then the condition written below will run */
        if (m == 45)
            System.out.println("Quarter to " + a[h + 1]);// displaying Time when m=45

        /** displaying Time when 30<m<60 but m does not equal to 45 */
        else if (m > 30 && m < 60)
            System.out.println(a[60 - m] + " minutes to " + a[h + 1]);
    }
}


Output Window:



Output Window

If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of codefeel free to comment below. I hope that my code is understandable. Please Subscribe to Programming Chaska for the latest programming updates and also to inspire me to write such unique and different programs. 
Thank You for Visiting 😄

Post a Comment

0 Comments