Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with calculating age

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help with calculating age

    Hello all, I'm in the process of working on a program for my Java programming class. Its done, but I wanted to add another feature to it. Right now, it calculates your age based on the year you were born. I wanted to add the feature that it also looks at the month, then the date it is currently to figure out your age.

    For example, lets say you were born 11/12/1985. Right now, it only cares about 1985 and thinks your age is 29. Even though, your birthday hasn't come yet.

    I wanted to figure out a way to use the rest of that date to tell your age. Trouble is, I'm not sure where to start for that. Is there a way to import the current date from your computer to the program? Do you have to put the date in manually?

    Is there a standard way of making this calculation?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with calculating age

    You can use the Calendar class to check if today's month/day is before or after the month/day of the birthdate and adjust accordingly.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculating age

    how do you that? Currently, I'm using separate integers to store the month, the date, and the year.

    For example, if its 9/14/2014 I'm using one int for 9, one for 14, and one for 2014.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with calculating age

    A Calendar object will represent the exact point in time it was created. You can then call its methods to find the day and month. Then compare them to you birthdate month and day. Go to the API and read about the Calendar class. Google for code examples.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with calculating age

    Thanks, what is api?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with calculating age

    Quote Originally Posted by mattig89ch View Post
    Thanks, what is api?
    Google "java calendar doc" and the API will be the top results.

Similar Threads

  1. Hi from a new old (in age) programmer
    By PeterPeterson in forum Member Introductions
    Replies: 3
    Last Post: September 7th, 2014, 05:10 PM
  2. Pi calculating help.
    By Unitize in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 6th, 2014, 11:40 AM
  3. [SOLVED] How do I sort a TreeSet<Person> by Person's age?
    By bihlas in forum Java Theory & Questions
    Replies: 2
    Last Post: August 2nd, 2014, 04:00 AM
  4. Calculating pi
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2013, 08:25 AM
  5. Age Calculator
    By Sagittarian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 1st, 2012, 08:06 PM