# Anyone good at math?

• 01-02-2009, 06:37 AM
iWonder
Anyone good at math?
I have a program to do, and i figured that since i have the code working properly, i shouldn't post it in the new to java section..
anyways..
we are supposed to get the current date, and get the date of which the user was born on and the program is supposed to tell the user how many DAYS they have lived and the hours they have slept.
we are told to make years=365 days, months=30 days.
and we also assume they sleep 8 hours a night (but that part is easy, just multiply the days by 8)

anyone good at math who knows how to get this?
my code doesnt really need any editing, so theres no need to post it..
i just suck at math xD
• 01-02-2009, 06:50 AM
Eranga
What you want to do exactly. Convert the date into days?
• 01-02-2009, 07:07 AM
mtyoung
you can use calendar class
create 2 calendar object, one for instant, one for the inputted date
and use functions like cal.get(cal.YEAR) to calulate
year diff, month diff, and day diff
• 01-02-2009, 07:27 AM
iWonder
Hm, we havent really learned about that yet,
is there any other way?

And yes, I want to convert the date to days
• 01-02-2009, 07:55 AM
Eranga
If you are using that assumptions why it's difficult? I mean you have two dates something like this, 1980/10/23 and the current date. So you have to deduct from each other.

But you have to validate few cases before doing deduct. Days must be greater than 30, month must be greater than 12.

But it's not easy to write in to the code. Use if the Calender is much effective.
• 01-02-2009, 08:04 AM
mtyoung
learnt?....

then do you know Date class and SimpleDateFormat?
Construct new Date object
use format method in SimpleDateFormat to format it...

do you know String class
use parse method to get current year, month, date...

compute the year/month/day diff
• 01-02-2009, 08:56 PM
iWonder
ultimately, all i want help with is the MATH,
im not strong in math so i want to know how to get the differences of the years?
without talking about java, i just want to know the method of how to obtain the # of days someone has been alive for given the current date and the date they were born..
something like add this, subtract that..kind of thing
• 01-03-2009, 02:33 AM
mtyoung
assume the format of date is yyyy/MM/dd
current date is 2009/01/03 and DOB is 1957/09/13
then
diff is current - DOB
let consider month case
1 - 9 = -8
because of 1 year = 12 months
difference in month is 1 - 9 + 12 = 4
and so on...
• 01-03-2009, 05:01 AM
iWonder
so, from the 4, how do i get the amount of days someone has lived?
• 01-03-2009, 05:06 AM
mtyoung
Quote:

we are told to make years=365 days, months=30 days. sleep 8 hours a night
if diff is 3 year, 4 month, 10 days
(3 * 365 + 4 * 30 + 10) * 8
remind carry in calculation
• 01-03-2009, 11:37 AM
mtyoung
8 more hours may needed
as 14/10/2008, 16/10/2008 diff in 2 days
but 14, 15 and 16 are 3 days
• 01-08-2009, 09:35 AM
fishtoprecords
Quote:

Originally Posted by iWonder
anyone good at math who knows how to get this?
i just suck at math xD

I have a degree in Mathematics. You are not asking about math, maybe arithmetic, but really you just need to learn how to use the APIs for the date classes. or look at JodaDates.

The Java Date classes from Java 1.0 leave a lot to be desired. They have been patched and improved a little, but still are pretty inconsistent.
• 01-11-2009, 05:12 PM
Nicholas Jordan
yep,,...it says to use Calander class.