# Time calculations

• 09-29-2011, 09:18 PM
Jason
Time calculations
Ok this is homework, i have been going at this problem for a while and feel i almost have it. The way the code is structured is exactly how the assignment should be , per my professor. I have to calculate current time in my Time() ctor and calculate time elapsed in my Time(long elapsedTime) ctor, this has to be in milliseconds since the epoch, but i have to calculate elapsed time from Time(555550) as seen in my main class, i am not sure if i am calculating this properly, thanks for any input.

Time class:

Code:

```package chapter_10; public class Time {     private long hour;     private long minute;     private long second;     public Time() {       second = System.currentTimeMillis();       minute = second / 60;       hour = minute /60;     }       public Time(long elapsedTime){       long millisecond = System.currentTimeMillis();       second = millisecond / 1000;       minute = second / 60;       hour = minute /60; //    equate for current time.       second = second %60;       minute = minute %60;       hour = hour %24;           }         public long getHour() {         return hour;     }     public long getMinute() {         return minute;     }     public long getSecond() {         return second;     }         @Override     public  String toString(){       return getHour() + ":" + getMinute() + ":" + getSecond();   } }```
Main Class:

Code:

```package chapter_10; public class Ten_One {     public static void main(String[] args) {                 Time testTime = new Time();         Time testTime2 = new Time(555550);         System.out.println(testTime.toString());         System.out.println(testTime2.toString());     }    }```
• 09-29-2011, 09:27 PM
doWhile
Re: Time calculations
Code:

` second = System.currentTimeMillis();`
currentTimeMillis() == milliseconds != seconds
• 09-29-2011, 09:33 PM
Jason
Re: Time calculations
ok so then this would be the correct approach...

long millisecond = System.currentTimeMillis();
second = millisecond %60;
• 09-29-2011, 09:36 PM
Fubarable
Re: Time calculations
Quote:

Originally Posted by Jason
ok so then this would be the correct approach...

long millisecond = System.currentTimeMillis();
second = millisecond %60;

No. A simple program would show you that this doesn't work (if logic doesn't already tell you), and you will in fact want to test your code with your Java compiler before asking here as that would give you this answer quicker than we can, and not waste our time or yours.
• 09-29-2011, 09:38 PM
Jason
Re: Time calculations
ok i have been but i will continue to do so, thanks.
• 09-29-2011, 09:39 PM
Fubarable
Re: Time calculations