Respuesta :
Answer:
import java.util.Scanner;
class Time
{
int hour,min,sec;
Time()
{
long millisecs=System.currentTimeMillis();
sec=(int)millisecs%60;
min=(int)(millisecs/60)%60;
hour=(int)(millisecs/(60*60))%24;
}
Time(long millisecs)
{
sec=(int)millisecs%60;
min=(int)(millisecs/60)%60;
hour=(int)(millisecs/(60*60))%24;
}
Time(int hour,int min,int sec)
{
this.hour=hour;
this.min=min;
this.sec=sec;
}
void readTime()
{
Scanner s=new Scanner(System.in);
System.out.println("Type Hours : ");
hour=s.nextInt();
System.out.println("Type mins : ");
min=s.nextInt();
System.out.println("Type sec : ");
sec=s.nextInt();
}
void setTime(long ellapseTime)
{
sec=(int)ellapseTime%60;
min=(int)(ellapseTime/60)%60;
hour=(int)(ellapseTime/(60*60))%24;
}
void showTime()
{
System.out.println("Time is(hh:mm:ss) - "+hour+":"+min+":"+sec);
}
}
class ShowCurrentTime
{
public static void main(String args[])
{
Time t1=new Time();
Time t2=new Time(555550000);
Time t3=new Time(4,46,34);
t1.showTime();
t2.showTime();
t3.showTime();
}
}
Explanation:
- Initialize the seconds, miliseconds, minutes and hours in the constructor of the Time class.
- Take the hour, minute and seconds as an input from user inside the readTime method.
- Calculate the hour, minute and seconds inside the setTime method.
- Inside the main method, create object of the Time class and call the showTime method to display the time.