Results 1 to 7 of 7
  1. #1
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default 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. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Time calculations

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

  3. #3
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Time calculations

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

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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Time calculations

    Quote Originally Posted by Jason View Post
    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. #5
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Time calculations

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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default 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.

Similar Threads

  1. Simple double calculations...
    By b0rt in forum New To Java
    Replies: 8
    Last Post: 08-31-2011, 04:01 PM
  2. Rounding calculations
    By lynxbci in forum New To Java
    Replies: 9
    Last Post: 08-20-2011, 04:49 PM
  3. Thread calculations, and another thread to use the calculations
    By phil128 in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-12-2010, 05:32 PM
  4. [SOLVED] Simple Calculations in Java
    By fullmetaljacket in forum New To Java
    Replies: 9
    Last Post: 05-19-2009, 04:19 AM
  5. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 04:36 AM

Posting Permissions

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