Cómo puedo calcular la edad (incluyendo día y mes) mediante un EditText? Ahora mismo lo hago mediante un Calendar, pero me gustaría que al escribir la fecha 23/01/2017(Ejemplo) en el EditText se calcule la edad.
EditText que quiero usar para calcular la edad:
editText1=(EditText) findViewById(R.id.editText1);
MainActivity:
public class MainActivity extends Activity implements OnClickListener{
private Button btnStart;
static final int DATE_START_DIALOG_ID = 0;
private int startYear=1970;
private int startMonth=6;
private int startDay=15;
private AgeCalculation age = null;
private TextView currentDate;
private TextView birthDate;
private TextView result;
EditText editText1;
TextView edad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
age=new AgeCalculation();
currentDate=(TextView) findViewById(R.id.textView1);
currentDate.setText("Current Date(DD/MM/YY) : "+age.getCurrentDate());
birthDate=(TextView) findViewById(R.id.textView2);
result=(TextView) findViewById(R.id.textView3);
edad=(TextView) findViewById(R.id.edad);
editText1=(EditText) findViewById(R.id.editText1);
btnStart=(Button) findViewById(R.id.button1);
btnStart.setOnClickListener(this);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
startYear, startMonth, startDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener
= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
startYear=selectedYear;
startMonth=selectedMonth;
startDay=selectedDay;
age.setDateOfBirth(startYear, startMonth, startDay);
birthDate.setText("Date of Birth(DD/MM/YY): "+selectedDay+":"+(startMonth+1)+":"+startYear);
calculateAge();
}
};
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
showDialog(DATE_START_DIALOG_ID);
break;
default:
break;
}
}
private void calculateAge()
{
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
Toast.makeText(getBaseContext(), "click the resulted button"+age.getResult() , Toast.LENGTH_SHORT).show();
result.setText(age.getResult()+(" Años"));
String[] dayMonthYear = age.getResult().split(":");
String year = dayMonthYear[2];
edad.setText(year + " Años");
}
}
AgeCalculation:
public class AgeCalculation {
private int startYear;
private int startMonth;
private int startDay;
private int endYear;
private int endMonth;
private int endDay;
private int resYear;
private int resMonth;
private int resDay;
private Calendar end;
public String getCurrentDate()
{
end=Calendar.getInstance();
endYear=end.get(Calendar.YEAR);
endMonth=end.get(Calendar.MONTH);
endMonth++;
endDay=end.get(Calendar.DAY_OF_MONTH);
return endDay+":"+endMonth+":"+endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay)
{
startYear=sYear;
startMonth=sMonth;
startMonth++;
startDay=sDay;
}
public void calcualteYear()
{
resYear=endYear-startYear;
}
public void calcualteMonth()
{
if(endMonth>=startMonth)
{
resMonth= endMonth-startMonth;
}
else
{
resMonth=endMonth-startMonth;
resMonth=12+resMonth;
resYear--;
}
}
public void calcualteDay()
{
if(endDay>=startDay)
{
resDay= endDay-startDay;
}
else
{
resDay=endDay-startDay;
resDay=30+resDay;
if(resMonth==0)
{
resMonth=11;
resYear--;
}
else
{
resMonth--;
}
}
}
public String getResult()
{
return resDay+":"+resMonth+":"+resYear;
}
}