Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:13:28+00:00 2026-06-01T16:13:28+00:00

Possible Duplicate: How do I calculate someone's age in Java? I want to calculate

  • 0

Possible Duplicate:
How do I calculate someone's age in Java?

I want to calculate the user age , and my method doesnt give the correct age in case if the month of birth is equal to the current month and the day of birth less than or equal the current day ( if the user enter his birth date through the date picker as 9/4/1990 or 4/4/1990 the age will be 21 not 22) how i can solve this problem ? what is the update i should do to get the correct age in this case ? please help me ….

this is my method

public static  String getAge(int year,int month,int day) {
    Calendar dob = Calendar.getInstance(); 
    Calendar today = Calendar.getInstance();

    dob.set(year, month, day); 
    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
        age--; 
    }
    Integer ageInt = new Integer(age);
    String ageS = ageInt.toString();

    return ageS;  
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:13:30+00:00Added an answer on June 1, 2026 at 4:13 pm

    There are two problems with your code:

    • if the date of birth is 9th April 1990, you need to write dob.set(1990,3,9) as months start at 0 ==> you probably need dob.set(year, month - 1, day);
    • if the current year is a leap year and not the year of birth (or vice versa) and dates are after the 28/29 Feb, you will get 1 day difference on an identical date.

    This seems to work but you should test it with various scenarios and make sure you are happy with the result:

    public static String getAge(int year, int month, int day) {
        Calendar dob = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
    
    
        dob.set(year, month - 1, day);
    
        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) {
            age--;
        } else if(today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)) {
            if (today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH)) {
                age--;
            }
        }
    
        Integer ageInt = new Integer(age);
        String ageS = ageInt.toString();
    
        return ageS;
    
    }
    

    And a (very simplified) test:

    public static void main(String[] args) { //today = 8 April 2012
        System.out.println(getAge(1990,3,7)); //22
        System.out.println(getAge(1990,3,8)); //22
        System.out.println(getAge(1990,3,9)); //22
        System.out.println(getAge(1990,4,7)); //22
        System.out.println(getAge(1990,4,8)); //22
        System.out.println(getAge(1990,4,9)); //21
        System.out.println(getAge(1990,5,7)); //21        
        System.out.println(getAge(1990,5,8)); //21        
        System.out.println(getAge(1990,5,9)); //21        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I calculate someone's age in Java? hi friends i want
Possible Duplicate: How do I calculate someone’s age in C#? I want to calculate
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How do I calculate someone's age based on a DateTime type birthday?
Possible Duplicate: Calculate age in JavaScript In some point of my JS code I
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
Possible Duplicate: Measure execution time for a Java method I have an application by
Possible Duplicate: How do I calculate relative time? I want to convert Date Value
Possible Duplicate: how to calculate the current speed and average speed of user travelling
Possible Duplicate: Calculate week of month in .NET I need to calculate the week

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.