Results 1 to 7 of 7
  1. #1
    rocky86 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Help in calculating time different!

    hi guys! i need help on calculating about the time differences in 24hour format!! A program that reads in start time, end time then output the time difference!!

    example: Enter Start time: 2355
    Enter End time : 0005
    Time difference is 10min

    I have no idea how to do that!! I understand I can take end time to minus the start time, however that don't give me the time differences!!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    How would you do this on paper? Write out the steps you do in your head to calculate the answer. Then start translating those steps to code.

    You might want to take a look at the Calendar class. But chances are, this is for an assignment, and you're supposed to come up with your own algorithm.

  3. #3
    rocky86 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    basically what my program do is ask for user input! after that it print out the time differences between start and end! if the user enter -1 it will terminate the program! however! if user enter

    Start time:2311
    end Time: 2321
    Time difference is 10min

    This output is correct! BUT

    Start time:0021
    end Time: 0011
    Time difference is 2350min

    that wrong!THE time difference should be 1430min but I got no idea how to calculate to make it !

    this is my code:

    Java Code:
    int start=0, end=0, hour=0, diffmin=0,min=2360;
    
    System.out.print("Enter starting time: ");
    start = sc.nextInt();
    
         
    while(start!=-1)
    {
    
    System.out.print("Enter ending time: ");
    end = sc.nextInt();
    
    if(end>start)
    {
    diffmin = end - start;
        
    }
    else if(end<start)
    {
    end+=min;
    diffmin=end-start;
    }
    System.out.println("Time difference is "+diffmin+ " min");
    start=0;//reset to 0
    end=0;//reset to 0
    System.out.print("Enter starting time: ");
    start = sc.nextInt();
    
    }
    
    System.out.print("<Program terminates>");
    
    
    }
    
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    Quote Originally Posted by rocky86 View Post
    basically what my program do is ask for user input! after that it print out the time differences between start and end! if the user enter -1 it will terminate the program!
    Okay!


    Quote Originally Posted by rocky86 View Post
    Start time:0021
    end Time: 0011
    Time difference is 2350min

    that wrong!
    Why? If it is 12:21 AM, the next time it will be 12:11 AM is in 23 hours and 50 minutes.

    Quote Originally Posted by rocky86 View Post
    THE time difference should be 1430min but I got no idea how to calculate to make it !
    Hm, I see. Are you sure you fully understand the requirements? It seems strange to do it that way. Assuming you do have the requirements down, again, think about how you do this in your head: if something is 23 hours and 50 minutes in the future, how did you figure out that it is 1,430 minutes away?

  5. #5
    rocky86 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    erm! the 1430min is a example given by my assignment!

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    I guess I'm confused about the requirements. Is the time input in HH:MM (or HHMM) format? Or is it just a basic count of minutes? Does 0100 represent 1:00 AM, or is it 100 minutes into the interval? You seem to be switching back and forth between the two formats.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    If Tend < Tstart we can assume that Tend is in the next day; so there are 24 hours - Tstart+Tend minutes between the two times, i.e. 1440 minutes - Tstart +Tend (all measured in minutes relative to midnight)

    kind regards,

    Jos

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. calculating time of program runing
    By sara12345 in forum New To Java
    Replies: 3
    Last Post: 03-17-2010, 12:25 AM
  3. Calculating the Running time
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 10:32 PM
  4. Need help with calculating the time the user...
    By Smirre in forum New To Java
    Replies: 8
    Last Post: 11-20-2008, 01:15 PM
  5. Calculating the DB connection time
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:55 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
  •