By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

Small Calculation Help

H0kage
P: 6
I am Suppose To Create A Program That We Enter A Number Of Seconds Like

95362875

And Return To Me that number in the following way

Years:3
Weeks:1
Days:1
Hours:17
Minutes:41
Seconds:15

Its Suppose To Be In GUI Design which i have already made and its working my problem is the calculations i am suppose to enter in the action performed section of the button.
Dead Line Is Tommorow Morning 9.00am GMT+2

Thank You In Advance
Dec 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: 26
I am Suppose To Create A Program That We Enter A Number Of Seconds Like

95362875

And Return To Me that number in the following way

Years:3
Weeks:1
Days:1
Hours:17
Minutes:41
Seconds:15

Its Suppose To Be In GUI Design which i have already made and its working my problem is the calculations i am suppose to enter in the action performed section of the button.
Dead Line Is Tommorow Morning 9.00am GMT+2

Thank You In Advance
normally i wouldn't help as the deadline is so close. but i was bored. The trick is to work out the remainders using modulo (%) and then use a simple division to get the amounts.

hence:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public void timeConvert(int secs) {
  3. //         int secs = 95362875;
  4.  
  5.         // first set up the constant values for time.
  6.         int YearSecVal = 60 * 60 * 24 * 7 * 52;
  7.         int WeekSecVal = 60 * 60 * 24 * 7;
  8.         int DaySecVal = 60 * 60 * 24;
  9.         int HourSecVal = 60 * 60;
  10.         int MinSecVal = 60;   
  11.  
  12.         // next work out the remainding seconds from each calc.
  13.         int newWeekSecs = secs % YearSecVal;
  14.         int newDaySecs = newWeekSecs % WeekSecVal;
  15.         int newHourSecs = newDaySecs % DaySecVal;
  16.         int newMinSecs = newHourSecs % HourSecVal;
  17.         int newSecs = newMinSecs % MinSecVal;
  18.  
  19.         // print it all out!
  20.         System.out.println(secs / YearSecVal + " years");
  21.         System.out.println(newWeekSecs / WeekSecVal + " weeks");
  22.         System.out.println(newDaySecs / DaySecVal + " days");
  23.         System.out.println(newHourSecs / HourSecVal + " hours");
  24.         System.out.println(newMinSecs / MinSecVal + " mins");
  25.         System.out.println(newSecs + " secs");
  26.  
  27.     }
  28.  
  29.  
and im also hoping your deadline has passed now :|

Theres prob a much nicer recursive way to do this, but simplicity was never a strong point.
Dec 8 '06 #2

H0kage
P: 6
Thank You M8 your help is appreciated.Yup I missed the deadline but at least i will understand its function.
Dec 11 '06 #3

10K+
P: 13,264
Thank You M8 your help is appreciated.Yup I missed the deadline but at least i will understand its function.
And hopefully you'll post your problems much earlier too. Sorry you did not get a response on time though. I was away myself at the time.
Dec 11 '06 #4

H0kage
P: 6
Like you say i will try to post earlier but i wanted it to fight it till the last day
before the end since i dont like to get ready solutions.I want Tips and guidance :)
Dec 17 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.