> I am lookinf for an algorithm that will count the
> age of the students according to their date of birth.
> Also i want the program to extract the age of the
> students automatically in the next years.....
> thank you
Keep adding 1 year to the date of birth until the resultant date is after yesterday. The age is the total number of years added.
/*
* AgeCalculator.java
*
* Created on June 26, 2007, 8:01 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author k.rajadurai
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class AgeCalculator
{
public AgeCalculator()
{
String dob = "04/30/1982";
String admitDate = "06/27/2007";
try
{
System.out.println(calculateAge(dob,admitDate));
}
catch(Exception e)
{
System.out.println("Error " + e.getMessage());
}
}
public static String calculateAge(String dob, String dateOfAdmission) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date birthDate = sdf.parse(dob);
Date admissionDate = sdf.parse(dateOfAdmission);
Calendar calendarBirth = Calendar.getInstance();
calendarBirth.setTime(birthDate);
Calendar calendarDateOfAdmission = Calendar.getInstance();
calendarDateOfAdmission.setTime(admissionDate);
int days = 0;
// Checking the Birth date is coming after the Admission Date or not. If Yes then return empty string. Otherwise Process the Following
if(calendarBirth.before(calendarDateOfAdmission))
{
//Checking the Years and Months of both Dates are same or not. If yes then Calculate Only Days. Otherwise Process the Else Pert
if(calendarDateOfAdmission.get(Calendar.YEAR) == calendarBirth.get(Calendar.YEAR) && calendarDateOfAdmission.get(Calendar.MONTH) == calendarBirth.get(Calendar.MONTH))
{
days = calendarDateOfAdmission.get(Calendar.DATE) - calendarBirth.get(Calendar.DATE);
return days + " Day(s)";
}
else
{
// calculate age as the difference in years.
int age = calendarDateOfAdmission.get(Calendar.YEAR) - calendarBirth.get(Calendar.YEAR);
int month = 0;
// if that date has not occurred yet, subtract one from age
calendarBirth.set(Calendar.YEAR, calendarDateOfAdmission.get(Calendar.YEAR));
if (calendarDateOfAdmission.before(calendarBirth))
{
age = age - 1;
month = calendarBirth.get(Calendar.MONTH) - calendarDateOfAdmission.get(Calendar.MONTH);
month = 12 - month;
// if that date has not occurred yet, subtract one from month
calendarBirth.set(Calendar.MONTH, calendarDateOfAdmission.get(Calendar.MONTH));
if (calendarDateOfAdmission.before(calendarBirth))
{
month = month - 1;
}
}
else
{
month = calendarDateOfAdmission.get(Calendar.MONTH) - calendarBirth.get(Calendar.MONTH);
// if that date has not occurred yet, subtract one from month
calendarBirth.set(Calendar.MONTH, calendarDateOfAdmission.get(Calendar.MONTH));
if (calendarDateOfAdmission.before(calendarBirth))
{
month = month - 1;
}
}
return age + " Year(s) " + month + " Month(s)";
}
}
// If the Birth Date is coming after the Admission Date then return Empty String
else
{
return "";
}
}
public static void main(String[] args)
{
new AgeCalculator();
}
}