Monday, January 16, 2017

Program to display time in 12 and 24 hours formats.

import java.util.*;
class Watch
{
int second,minute,hour;
void store()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the time in 24 format: ");
System.out.print("Hours : ");
do
{
hour=sc.nextInt();
if(hour > 23 || hour < 0)
{
System.out.println("Oop! Hours must be between 0 to 23");
System.out.print("Hours : ");
}
}while(hour > 23 || hour < 0);
System.out.print("Minutes : ");
do
{
minute=sc.nextInt();
if(minute > 59 || minute < 0)
{
System.out.println("Oop! Minutes must be between 0 to 59");
System.out.print("Minutes : ");
}
}while(minute > 59 || minute < 0);
System.out.print("Seconds : ");
do
{
second=sc.nextInt();
if(second >59 || second < 0)
{
System.out.println("Oop!Seconds must be between 0 to 59");
System.out.print("Seconds : ");
}
}while(second > 59 || second < 0);
}
void show_time12F()
{
if(hour > 12)
{
System.out.println(" 12 Format Time -> "+(hour-12)+":"+minute+":"+second+" PM");
}
else if (hour == 12)
{
System.out.println(" 12 Format Time -> "+hour+":"+minute+":"+second+" PM");
}
else
{
System.out.println(" 12 Format Time -> "+hour+":"+minute+":"+second+" AM");
}
}
void show_time24F()
{
System.out.println(" 24 Format Time -> "+hour+":"+minute+":"+second);
if(hour==0)
{
System.out.println("It's Mid Night!!!!!!\n");
}
else if(hour>1 && hour<11)
{
System.out.println("It's Morning!!!!!!\n");
}
else if(hour>11 && hour<12)
{
System.out.println("It's Morning!!!!!!\n");
}
else
{
System.out.println("It's Evening!!!!!!\n");
}
}
public static void main(String...args)
{
Watch w1=new Watch();
w1.store();
w1.show_time12F();
w1.show_time24F();
}
}

                                                                                                                          

Output:


No comments:

Post a Comment