Results 1 to 4 of 4
  1. #1
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default a constructor to convert seconds to hours, min & seconds

    hey everyone,

    I am trying to figure out how to make the following: converting a value of seconds and convert it to the amount of hours,minutes and seconds it is.
    For instance 7200 sec would give me 2 hours, 0 minutes and 0 seconds.
    I know it is something with % but I can't figure out how to do it.
    The code I started out with looks like this:

    public Time(int totSeconds){
    something like ==> int out = totSeconds % 3600; this gives me the rest but not the amount of hours :s
    ...

    if anyone can help me, thanks in advance :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    This is simpler than math 101: there are 3600 seconds in an hour, you have 7200 seconds, so you have 7200/3600 == 2 hours.

    kind regards,

    Jos

  3. #3
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default

    but how does it work with minutes and seonds? for example 7350 seconds?
    if you devide then there is a rest somewhere but where?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by senca View Post
    but how does it work with minutes and seonds? for example 7350 seconds?
    if you devide then there is a rest somewhere but where?
    7350/3600 == 2
    7350%3600 == 150

    kind regards,

    Jos

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2010, 01:47 PM
  2. Convert microseconds to seconds
    By Basit56 in forum New To Java
    Replies: 4
    Last Post: 12-31-2009, 05:54 AM
  3. Difference in seconds between two times
    By jazzy639 in forum New To Java
    Replies: 11
    Last Post: 09-26-2009, 06:58 PM
  4. getting time elapsed in seconds
    By ali_sakar in forum New To Java
    Replies: 3
    Last Post: 03-06-2009, 08:37 AM
  5. Need help with calculate the number of seconds
    By ProUnbeatable in forum New To Java
    Replies: 5
    Last Post: 10-11-2008, 03:07 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
  •