its an age calculator project I need to know how to convert multiple EditText values into one date String in an android studio? keep in mind I am using "joda-time library" it's not showing the result. I don't know where I did a mistake! I have forgotten everything I can not fix now hope you guys can help me with that. thanks
public void dateOfBirth(){
String day = editTextDay.getText().toString().trim();
String month = editTextMonth.getText().toString().trim();
String year = editTextYear.getText().toString().trim();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sDate = day+"/"+month+"/"+year;
long date = System.currentTimeMillis();
String eDate = simpleDateFormat.format(date);
try {
Date date1 = simpleDateFormat.parse(sDate);
Date date2 =simpleDateFormat.parse(eDate);
/* long eDate = System.currentTimeMillis();
Date date2 = simpleDateFormat.parse(String.valueOf((eDate)));*/
long startDate = date1.getTime();
long endDate =date2.getTime();
if (startDate<=endDate){
Period period = new Period(startDate, endDate, PeriodType.yearMonthDay());
int years = period.getYears();
int months =period.getMonths();
int days = period.getDays();
textViewDay.setText(days);
textViewMonth.setText(months);
textViewYear.setText(years);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
Go all-in on Joda-Time
When I ran the above snippet just now, the output was:
Since you are interested in years, months and days, you don’t need
DateTimeobjects (though they would work). They include time of day too. Just useLocalDate.The classes
SimpleDateFormatandDateare poorly designed and long outdated, the former in particular notoriously troublesome. I recommend you stay away from those, and also from representing a point in time as alongcount of milliseconds since the epoch. The good choices are: