2014-06-09 211 views
0

我想要在android中执行一个应用程序,例如,如果我们编写应用程序给出的某些任务作为通知,如果currenttime=timewespeciefied. 我已经有一个包含用户指定时间的数组。 这里的问题是计算剩余时间! 是否有一个计算剩余时间的类? 在我的情况下,我有多个任务,所以我必须检查表中任务的时间是否已完成。Android计算剩余时间

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; 
      } 

有多种情况!我如何做到这一点。

+0

你可以举一个你想要的输出类型的例子吗?例如,一个表示“1小时43分钟”的字符串? –

+0

例如:现在时间01:10,用户在02:03有一个任务,输出必须是:剩余的时间是:0h 53minute @DanS – HinoHara

+0

这不是我想要的!我想计算剩下的时间,我有一个数组,例如5或6个任务(时间),然后我必须将当前时间与任务表 – HinoHara

回答

2

String产生的结果为DateUtils中的静态函数可以完成您的任务。例如:

// 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" 
+0

谢谢你,我正在寻找什么! – HinoHara