0

I want to do an app in android for example if we program some tasks the app give as notification if the currenttime=timewespeciefied. I have already an array that contain the time the user specified. the problem here is to calculate the remaining time ! is there a class that calculate the remaining time ? in my case I have multiple tasks so I must check if the time in the task in the table is already done or not.

void LeftTime(){
currenttime.settonow();
            Time LeftTime=new Time();
            for (int i = 0; i <TaskTable.length ; i++) {
                if(currenttime.hour<=TaskTable[i].hour)
                    if(TaskTable.minute==0) LeftTime.minute=60-TaskTable[i].minute;
                if(TaskTable[i].minute<currenttime.minute)
                    LeftTime.minute=currenttime-Tasktable[i].minute;
                            else
                    LeftTime.minute=60-currenttime+Tasktable[i].minute;
            }

There is multiple Cases ! How I can do that.

HinoHara
  • 572
  • 3
  • 13
  • 24
  • Can you give an example of the kind of output you want? For example, a `String` that says "1 hour and 43 minutes"? – Dan S Jun 10 '14 at 00:07
  • For example : the time now 01:10 , the user have a task in 02:03 the output must be : the remaining time is : 0h 53minute @DanS – HinoHara Jun 10 '14 at 00:11
  • it's not what I want ! I want to calculate the remaining time I have an array that contians for example 5 or 6 tasks (time) , then I must compare the current time with the table of tasks – HinoHara Jun 10 '14 at 00:27
  • Sorry, that was a mistaken click, I have retracted the erroneous vote and removed the mistaken comment. – Dan S Jun 10 '14 at 00:28
  • it's not a problem thank you for answer ! – HinoHara Jun 10 '14 at 00:30

1 Answers1

3

The family of String resulting static functions in DateUtils can accomplish your task. For example:

// Example 1
// Date then; // When the notification will occur
Date now = new Date();
String remaining1 = DateUtils.formatElapsedTime ((then.getTime() - now.getTime())/1000); // Remaining time to seconds
// remaining1: "MM:SS"

// Example 2
String remaining2 = DateUtils.formatElapsedTime (60 * 64 + 8); // 64 minutes in seconds and 8 seconds
// remaining2: "64:08"
Dan S
  • 9,139
  • 3
  • 37
  • 48
  • Thank you exactly what I'm I searching ! – HinoHara Jun 10 '14 at 00:30
  • Says Operator '-' cannot be applied to java.util.Date, java.util.Date. I was calculating the remaining time from a custom date to the current date. – Deepak J Jul 03 '20 at 07:25
  • @DeepakJ Don't forget to call `.getTime()` the `-` operator takes numeric primitives. Other languages allow overloading operators but not Java. – Dan S Jul 06 '20 at 23:04