Can someone help me with a short program that calculates age, the user will input their birthday and the program displays the age..

Go ahead and describe how your program should work and ask any questions you have about how to write the program.

THe program will ask the user to input their birthday, then it will automatically calculate and print back the age. That is what the program was supposed to do. I did searched a lot of codes but most of them are too long and not that easy to understand, well for a beginner like me..

Take it one step at a time.
First step: ask user for their birthday.
Second step: read in birthday

Try to write a program to do those two steps.

While you are writing the code for those two steps, think about the next steps the code has to do.

Edited 4 Years Ago by NormR1

static String errorMessage;

public static boolean isValidDate(String date)
{
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
    Date testDate = null;

    try
    { testDate=sdf.parse (date);  }
    catch (ParseException e)
    { errorMessage = "the date you provided is invalid date" + "format.";
      return false;
    }


    if (!sdf.format(testDate).equals(date)) 
            {
            errorMessage = "The date that you provided is invalid.";
            return false;
            }
            return true;
            }

AND THIS IS TO BE INSERTED ON THE MAIN PROGRAM, BUT I'M HAVING ERROR WITH THE currentDate...

if(isValidDate(Bdate))
    {

        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();

        int age = 0;
        int factor = 0; 

        Date date1 = new SimpleDateFormat("MM-dd-yyyy").parse(Bdate);
        Date date2 = new SimpleDateFormat("MM-dd-yyyy").parse(currentDate);
        cal1.setTime(date1);
        cal2.setTime(date2);

        if(cal2.get(Calendar.DAY_OF_YEAR) < cal1.get(Calendar.DAY_OF_YEAR))
        {
        factor = -1;        
        }

        age = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + factor;
        out.write("-" + age);
        out.write("\r\n");
        out.close();
    }

    else
    {
    System.out.println("Invalid");
    }

I'M HAVING ERROR WITH THE currentDate...

Please post the full text of the error message.

What does the code you just posted have to do with the first two steps of what your program is supposed to do?

Edited 4 Years Ago by NormR1

May be the below calculation will help u to convert the date to age

String Dob=txtDOB.getText().trim();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale("en", "AU"));
java.util.Date CurrentDate=new java.util.Date();
DateFormat FormattedDate=new SimpleDateFormat("dd/mm/yyyy");
String Currentformat=FormattedDate.format(CurrentDate);
Dob=Dob.substring(Dob.lastIndexOf("/")+1);
Currentformat=Currentformat.substring(Currentformat.lastIndexOf("/")+1);
String  Age=String.valueOf((Integer.parseInt(Currentformat))-(Integer.parseInt(Dob)));
txtAge.setText(Age);

... or maybe not.
@poojavb: if you post a lot of code like this please annotate it fully so that the learners here can understand it and learn from it. It may also be useful to post some sample input/output to confirm how well it works

Sure why not....

    String Dob=txtDOB.getText().trim(); 
    //get the date from the text box....trim is to remove any spaces if added

    java.util.Date CurrentDate=new java.util.Date(); 
    //declaring a variable which will store the current date

    DateFormat FormattedDate=new SimpleDateFormat("dd/mm/yyyy"); 
    //convert the date time to the format given

    String Currentformat=FormattedDate.format(CurrentDate); 
    //formatting the current date

    Dob=Dob.substring(Dob.lastIndexOf("/")+1);
    //get the index of the word from '/'

    Currentformat=Currentformat.substring(Currentformat.lastIndexOf("/")+1); 
    //get the index of the word from '/'

    String Age=String.valueOf((Integer.parseInt(Currentformat))-(Integer.parseInt(Dob))); 
    //calculating age with current date and the DOB

    txtAge.setText(Age); 
    //display the age in the Age text box

Another simple method but with static varibles

Calendar cal = new GregorianCalendar(1988, 2, 6); 
//Entering the data of birth

Calendar now = new GregorianCalendar(); 
//stores current dates value

int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR); 
//res will contain the caluclation for difference in years

if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)) || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {

        // if DOB's month is greater than current date month - means bday has not yet come so subtract res
        // or ( if the month is equal in both cases then consider the dates of the month... - if bday date is more than also subtract

 res--;
}

System.out.println(res);

Edited 4 Years Ago by poojavb

This question has already been answered. Start a new discussion instead.