Alexander Vasilchuk
Alexander Vasilchuk

Reputation: 155

How do I get a remaining time to finish activity?

How i can get remaining time from current time to a time i've installed? In my app I've been already using AlarmManager library and I've tried to write something like this:

    long remaining_hours = System.currentTimeMillis() / 1000 - _hour;
    long remaining_minutes = System.currentTimeMillis()/1000 - _minute;

but it is not working. Could you give me an example how to deal with it?

Upvotes: 0

Views: 40

Answers (1)

piotrek1543
piotrek1543

Reputation: 19361

Let's say you've written allready that code:

SimpleDateFormat dateFormatUCT = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
dateFormatUCT.setTimeZone(TimeZone.getTimeZone("UCT"));

String deadline = dateFormatUCT.format(new Date());

Now you will use the parse method of the same date format you created to change String back into a date:

Date deadlineDate = dateFormatUCT.parse(deadline);

To compare if another date (let's say: actualDate) has passed deadline, it sufficient to do:

if(actualDate.after(deadlineDate)) {
    // deadline has passed, do something
}

Then if you would like to get the time difference in milliseconds between them, ypu would write code like this:

long timeDiff = currentDate.getTime() - deadlineDate.getTime();

NOTICE: To convert this millisecond value to other units (like days, minutes, seconds etc) use methods of the TimeUnit class :

TimeUnit.SECONDS.toSeconds(timeDiff);

If you still have a question, please feel free to ask

Upvotes: 1

Related Questions