Java program to print time in Word

Java Program to print Time into words

This program print the Hours and minutes into its respective words.
// Program to print Time into words
import java.io.*;
class TimeInWords
{
    // Method displayTime started
    public void displayTime()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter hours.....(1-12)");  // Enter the hours 1 to 12
        int hh=Integer.parseInt(br.readLine());          // Enter the minutes 0 to 59
        System.out.println("Enter minutes.....(0-59)");
        int mm=Integer.parseInt(br.readLine());
     
      String time[]={"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"};
   
       if(hh>12 || mm>60)
            System.out.println("The Time is invalid");
        else if(mm==0)
            System.out.println("The time is "+time[hh-1]+" o'Clock");
        else if(mm<30 && mm!=15)
            System.out.println("The time is "+time[mm-1]+" minute(s) past "+time[hh-1]);
        else if(mm==15)
            System.out.println("The time is quarter past "+time[hh-1]);
        else if(mm==30)
            System.out.println("The time is "+time[hh-1] + " Thirty");
        else if(mm==45 && hh!=12)
            System.out.println("The time is quarter to "+time[hh]);
        else if(mm==45 && hh==12)
            System.out.println("The time is quarter to "+time[0]);
        else if(mm>30 && hh!=12)
            System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[hh]);
        else if(hh==12 && mm>30)
            System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[0]);

    } // Method displayTime ended

    public static void main(String args[]) throws IOException
    {
        TimeInWords ob = new TimeInWords();
        ob.displayTime();
    }
} // End of class


Output
Enter hours.....(1-12)
5
Enter minutes.....(0-59)
12
The time is Twelve minute(s) past Five

Enter hours.....(1-12)
7
Enter minutes.....(0-59)
50
The time is Ten minute(s) to Eight


SHARE THIS
Previous Post
Next Post