How can I calculate the age (including day and month) using an EditText? Right now I do it through a Calendar, but I would like that when writing the date 01/23/2017 (Example) in the EditText the age is calculated.
EditText that I want to use to calculate the age:
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;
}
}
You can extend the
EditText
and create aDateEditText
, which uses aInputFilter
to format and check the data as the user enters it.necessary changes:
Keep in mind when developing your regex that it has to allow not only the final input format, but also all partial inputs, such as:
After receiving the text of the EditText, you can convert your data using
SimpleDateFormat
:That's code I used for another type of
EditText
specialized, if you have problems with the regex, leave me a comment and I'll help you.added and corrected
The regex:
It should help you in the meantime, although it doesn't check if the data is valid in itself, it only guarantees that it works with the format.
Just in case, I recommend you
android:inputType="phone"
in the layout, because "number" doesn't give you the key for "/".The process that has occurred to me is:
You create an object
Date
in which the editText1 string is parsed.A one is created
LocaleDate
with the date and compared with today's