
Patrick B. answered 10/27/19
Math and computer tutor/teacher
import java.io.*;
class TimeCalc
{
public final int SECONDS_PER_MINUTE=60;
public final int MINUTES_PER_HOUR = 60;
public final int HOURS_PER_DAY = 24;
public final int DAYS_PER_YEAR = 365; //does not count leap years
private long numSeconds;
private int years;
private int days;
private int hours;
private int minutes;
private int seconds;
TimeCalc( long sec)
{
numSeconds = sec;
years = days=hours=minutes=seconds=0;
}
public void Calculate()
{
seconds = (int) (numSeconds % SECONDS_PER_MINUTE);
minutes = (int) (numSeconds / SECONDS_PER_MINUTE);
hours = minutes / MINUTES_PER_HOUR;
minutes = minutes % MINUTES_PER_HOUR;
days = hours / HOURS_PER_DAY;
hours = hours % HOURS_PER_DAY;
years = days / DAYS_PER_YEAR;
days = days % DAYS_PER_YEAR;
}
public void Output()
{
System.out.println(" # of seconds : " + numSeconds);
if (years>0)
{
System.out.print( years + " years ");
}
if (days >0)
{
System.out.print( days + " days ");
}
if ( hours > 0)
{
System.out.print( hours + " hours ");
}
if ( minutes >0)
{
System.out.print( minutes + " minutes ");
}
if (seconds >0)
{
System.out.println( seconds + " seconds ");
}
System.out.println("------------------------");
if (days >0)
{
System.out.print( days + " days ");
}
String outbuff = " ";
if (hours>0)
{
if (hours>9)
{
outbuff = outbuff + hours;
}
else
{
outbuff = outbuff + "0" + hours;
}
outbuff = outbuff + ":";
}
if (minutes>0)
{
if (minutes>9)
{
outbuff = outbuff + minutes;
}
else
{
outbuff = outbuff + "0" + minutes;
}
outbuff = outbuff + ":";
}
else
{
if (hours>0)
{
outbuff = outbuff + "00";
}
}
if (seconds >0)
{
if (seconds>9)
{
outbuff = outbuff + seconds;
}
else
{
outbuff = outbuff + "0" + seconds;
}
}
else
{
outbuff = outbuff + "00";
}
System.out.println(outbuff);
}
public void Go()
{
Console console = System.console();
String inbuff;
try
{
System.out.print(" Please input the number of seconds :>");
inbuff = console.readLine();
long numSeconds = Long.parseLong(inbuff);
TimeCalc myTimeCalc = new TimeCalc(numSeconds);
myTimeCalc.Calculate();
myTimeCalc.Output();
}
catch (Exception ex)
{
System.out.println(" Input exception has occured ");
}
} //Go
public static void main(String args[])
{
TimeCalc myTimeCalc = new TimeCalc(0);
myTimeCalc.Go();
}
} //class