birthdate class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: birthdate class

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    birthdate class

    I need to know how to prompt the user for their birth date and calculate and display how many days old they are. Can anyone help me out, because i am completely stuck, thanks in advance.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Do you want user input from the console or from a dialog window?

  3. #3
    Join Date
    Dec 2006
    Posts
    2
    user input from a dialog window would be best.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Use the methods of the Date and Calendar classes to represent the birthdate, today, and the difference between the two.

  5. #5
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    1
    import java.util.Calendar;
    import java.util.GregorianCalendar;

    public class AgeCalculator {

    public static void main(String[] args) {

    int dob_date = 7;
    int dob_month = 7;
    int dob_year = 1979;

    // Create a calendar object with the DOB
    Calendar dob = new GregorianCalendar(dob_year, dob_month, dob_date);

    // Create a calendar object with today's date
    Calendar today = Calendar.getInstance();

    int diff_year = (today.get(Calendar.YEAR) - dob.get(Calendar.YEAR));
    int diff_month = (today.get(Calendar.MONTH) + 1) - dob.get(Calendar.MONTH);
    int diff_date = (today.get(Calendar.DATE) - dob.get(Calendar.DATE));

    int age = 0;

    if(diff_year > 0){

    age = diff_year;

    if(diff_month < 0) age = age - 1;

    else if(diff_month == 0){

    if(diff_date < 0) age = age - 1;
    }
    }
    System.out.println("Age : " + age);
    }
    }

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 07:53 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center