Results 1 to 4 of 4
  1. #1
    yuchuang is offline Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    29
    Rep Power
    0

    Default What is the formula?

    I would like to ask what is the formula to use in our coding when we want to calculate 7384 seconds into hours, minutes and seconds? Thank you!

  2. #2
    levent Guest

    Default

    1 hour = 60 x 60 seconds
    1 minute = 60 seconds

    So you will first need to find hours by dividing your number to 60x60. Then you will divide the remaining seconds to 60 to find minutes and then you will assign remaining seconds as the seconds.


    Something like this:

    Java Code:
    int x = 7384;
    int hours = 7384/(60*60);
    int minutes = (7384%(60*60))/60;
    int seconds = (7384%(60*60))%60;

  3. #3
    yuchuang is offline Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    29
    Rep Power
    0

    Default

    Thanks! I understand now. :)

  4. #4
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    If you want to have more flexibility use convert() method from TimeUnit in Java SE 6
    Please have a look at the method details
    Java Code:
    convert
    
    public long convert(long sourceDuration,
                        TimeUnit sourceUnit)
    
        Convert the given time duration in the given unit to this unit. Conversions from finer to coarser granularities truncate, so lose precision. For example converting 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to Long.MIN_VALUE if negative or Long.MAX_VALUE if positive.
    
        For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)
    
        Parameters:
            sourceDuration - the time duration in the given sourceUnit
            sourceUnit - the unit of the sourceDuration argument 
        Returns:
            the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
    TimeUnit (Java Platform SE 6)

Similar Threads

  1. Replies: 0
    Last Post: 04-12-2008, 09:39 PM

Posting Permissions

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