Results 1 to 7 of 7
Thread: Time calculations
 09292011, 10:18 PM #1Member
 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(); } }
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()); } }
 09292011, 10:27 PM #2Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 7
Re: Time calculations
Java Code:second = System.currentTimeMillis();
 09292011, 10:33 PM #3Member
 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;

Re: Time calculations
 09292011, 10:38 PM #5Member
 Join Date
 Sep 2011
 Posts
 23
 Rep Power
 0
Re: Time calculations
ok i have been but i will continue to do so, thanks.

Re: Time calculations
with your logic above:
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.
 09292011, 10:42 PM #7Member
 Join Date
 Sep 2011
 Posts
 23
 Rep Power
 0
Similar Threads

Simple double calculations...
By b0rt in forum New To JavaReplies: 8Last Post: 08312011, 04:01 PM 
Rounding calculations
By lynxbci in forum New To JavaReplies: 9Last Post: 08202011, 04:49 PM 
Thread calculations, and another thread to use the calculations
By phil128 in forum Threads and SynchronizationReplies: 4Last Post: 12122010, 05:32 PM 
[SOLVED] Simple Calculations in Java
By fullmetaljacket in forum New To JavaReplies: 9Last Post: 05192009, 04:19 AM 
decimal calculations?
By arnab321 in forum CLDC and MIDPReplies: 5Last Post: 11192008, 04:36 AM
Bookmarks