Results 1 to 13 of 13
  1. #1
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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

  4. #4
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Hm, we havent really learned about that yet,
    is there any other way?

    And yes, I want to convert the date to days

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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.

  7. #7
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

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

  9. #9
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    so, from the 4, how do i get the amount of days someone has lived?

  10. #10
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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.

  11. #11
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    8 more hours may needed
    as 14/10/2008, 16/10/2008 diff in 2 days
    but 14, 15 and 16 are 3 days

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by iWonder View Post
    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.

  13. #13
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    yep,,...it says to use Calander class.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 03:32 PM
  2. Which framework is good?
    By felixtfelix in forum Web Frameworks
    Replies: 6
    Last Post: 08-16-2008, 12:48 AM
  3. Any Good e-book in JSF
    By felixtfelix in forum Web Frameworks
    Replies: 0
    Last Post: 05-08-2008, 05:18 PM
  4. any good java magazines around
    By dubdubdub in forum New To Java
    Replies: 18
    Last Post: 01-17-2008, 06:26 PM
  5. i'm no good in calling Method please help
    By lowpro in forum New To Java
    Replies: 1
    Last Post: 11-26-2007, 05:15 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
  •