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();
}
}
String day = "11";
String month = "4";
String year = "2012";
String sDate = "" + year + '-' + month + '-' + day;
LocalDate dob = new LocalDate(sDate);
LocalDate today = new LocalDate();
if (dob.isBefore(today)) {
Period period = new Period(dob, today, PeriodType.yearMonthDay());
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
System.out.println("" + years + " years " + months + " months " + days + " days");
}
When I ran the above snippet just now, the output was:
7 years 10 months 0 days
Since you are interested in years, months and days, you don’t need DateTime
objects (though they would work). They include time of day too. Just use LocalDate
.
The classes SimpleDateFormat
and Date
are 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 a long
count of milliseconds since the epoch. The good choices are: