1. Member
Join Date
Sep 2011
Posts
23
Rep Power
0

## 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:

Java 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:

Java 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());
}

}```

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9

## Re: Time calculations

Java Code:
` second = System.currentTimeMillis();`
currentTimeMillis() == milliseconds != seconds

3. Member
Join Date
Sep 2011
Posts
23
Rep Power
0

## Re: Time calculations

ok so then this would be the correct approach...

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

4. ## Re: Time calculations

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.

5. Member
Join Date
Sep 2011
Posts
23
Rep Power
0

## Re: Time calculations

ok i have been but i will continue to do so, thanks.

6. ## Re: Time calculations

1 millisecond would == 1 second.

And that doesn't make sense. But again, this you should discover for yourself as we are not a substitute for your compiler.

7. Member
Join Date
Sep 2011
Posts
23
Rep Power
0

## Re: Time calculations

Thank you, i do not expect anyone to be my compiler or do my work, what my compiler was out putting made no sense to me, hence why i asked.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•