Convert Time into Words in JAVA

Time Program in Java

Question : Given a time in the format of hh:mm (12-hour format) 0 < hh <=12, 0 <= mm < 60. The task is to convert it into words as shown:

For Example :-
 
Input                                              : hr = 5, min = 0
Output                                            : Five O' Clock
 
Input                                              : hr = 6, min = 24
Output                                            : Twenty Four Minutes Past Six


Terms to Remember :-

6:00 - six o'clock
6:10 - ten minutes past six
6:15 - quarter past six
6:30 - half past six
6:45 - quarter to seven
6:47 - thirteen minutes to seven




Program to convert time into words :-

Program :-

import java.io.*;
import java.util.*;
class Time
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int hr, min;
        String x,w;
        String words[] = {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen",
            "Sixteen","Seventeen","Eighteen","Nineteen","Twenty","Twenty one","Twenty two","Twenty three","Twenty four","Twenty five","Twenty six",
            "Twenty seven","Twenty eight", "Twenty nine"};
        System.out.println("Enter Hour");
        hr = in.nextInt();
        System.out.println("Enter Minutes");
        min = in.nextInt();
        if((hr>=1 && hr<=12)&&(min>=0&&min<=59))//checking valid condition of time entered
        {
            if(min==1 || min==59) //Condition to check whether to print Minute or Minutes
            {
                x="Minute";
            }
            else
            {
                x="Minutes";
            }
            if(hr==12) //Storing "One" in 'w' if hr is 12
            {
                w= words[1];
            }
            else //Storing Next value of hour in 'w' if hour is between 1-11
            {
                w= words[hr+1];
            }
            if(min==0)
            {
                System.out.println("Time Entered - "+hr+" : "+min);
                System.out.println(words[hr]+" O' clock");
            }
            else if(min==15)
            {
                System.out.println("Time Entered - "+hr+" : "+min);
                System.out.println("Quarter Past "+words[hr]);
            }
            else if(min==30)
            {
                System.out.println("Time Entered - "+hr+" : "+min);
                System.out.println("Half Past "+words[hr]);
            }
            else if(min==45)
            {
                System.out.println("Time Entered - "+hr+" : "+min);
                System.out.println("Quarter to "+w);
            }
            else if(min<30) //Condition for minutes between 1-29
            {
                System.out.println("Time Entered - "+hr+" : "+min);
                System.out.println(words[min]+" "+ x +" past "+words[hr]);
            }
            else //Condition for minutes between 30-59
            {
                System.out.println("Time Entered - "+hr+" : "+min);
                System.out.println(words[60-min]+" "+ x +" to "+w);
            }
        }//end of outer if statement
        else
        {
            System.out.println("Invalid Time Entered"); 
        }
    }// end of main method
}// end of class

 

For Proper Understanding Watch the Video :-





Watch this video : Convert Time into Words in JAVA

All the best :)
Keep Learning :)


Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA