maephisto
maephisto

Reputation: 5182

Android time interval percentage

I have three date objects - startDate, endDate, currentDate. They represent the start/end of a time interval, and the curent date. I would like to know what percentage does the currentDate represents (x), if currentDate is bewtween the startDate and endDate. enter image description here

Thank you!

Upvotes: 3

Views: 829

Answers (1)

CassOnMars
CassOnMars

Reputation: 6181

You can calculate the percentage by using .getTime() on each date:

double start = (double)StartDate.getTime();
double end = (double)EndDate.getTime();
double cur = (double)CurrentDate.getTime();

double percent = ((cur - start) / (end - start)) * 100f;

EDIT: added * 100f due to it being a percentage, not decimal...

Upvotes: 4

Related Questions