# Thread: Anyone good at math?

1. Member
Join Date
Nov 2008
Posts
30
Rep Power
0

## 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

2. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12
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

3. Member
Join Date
Nov 2008
Posts
30
Rep Power
0
Hm, we havent really learned about that yet,
is there any other way?

And yes, I want to convert the date to days

4. 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.

5. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12
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
Last edited by mtyoung; 01-02-2009 at 07:19 AM.

6. Member
Join Date
Nov 2008
Posts
30
Rep Power
0
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

7. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12
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...

8. Member
Join Date
Nov 2008
Posts
30
Rep Power
0
so, from the 4, how do i get the amount of days someone has lived?

9. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12
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
Last edited by mtyoung; 01-03-2009 at 04:14 AM.

10. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12
8 more hours may needed
as 14/10/2008, 16/10/2008 diff in 2 days
but 14, 15 and 16 are 3 days

11. 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.

12. yep,,...it says to use Calander class.

#### Posting Permissions

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