How I can find percentage between two dates compared to today in android/java? This question is based on this question.
I want to do something like this:
Calendar c = Calendar.getInstance();
yeartoday = c.get(Calendar.YEAR);
monthtoday = c.get(Calendar.MONTH);
daytoday = c.get(Calendar.DAY_OF_MONTH);
...
Date datestart = getDate(styear,(stmonth-1),stday);
Date dateend = getDate(year,(month-1),day);
Date datetoday = getDate(yeartoday,monthtoday,daytoday);
double percent = (((datetoday - datestart) / (dateend - datestart)) * 100);
...
public Date getDate(int year,int month,int day){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Date date = calendar.getTime();
return date;
}
That's what I did:
double millisstart = datestart.getTime();
double millisend = dateend.getTime();
double millistoday = datetoday.getTime();
double percent = (((millistoday - millisstart) / (millisend - millisstart)) * 100);
percentAsString = Double.toString(percent);
txt5.setText(percentAsString);