Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2007, 06:41 PM
Member
 
Join Date: Apr 2007
Location: Singapore
Posts: 30
yuchuang is on a distinguished road
Send a message via MSN to yuchuang
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2007, 07:21 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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:

Code:
int x = 7384; int hours = 7384/(60*60); int minutes = (7384%(60*60))/60; int seconds = (7384%(60*60))%60;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-30-2007, 08:02 PM
Member
 
Join Date: Apr 2007
Location: Singapore
Posts: 30
yuchuang is on a distinguished road
Send a message via MSN to yuchuang
Thanks! I understand now.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-30-2007, 11:00 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
If you want to have more flexibility use convert() method from TimeUnit in Java SE 6
Please have a look at the method details
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)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Computing the area of a triangle using Heron's Formula Java Tip java.lang 0 04-12-2008 09:39 PM


All times are GMT +3. The time now is 03:54 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org