我想从timepicker中扣除的小时数+分钟数中减去System.currentTimeMillis()
。但是,与转换为System.currentTimeMillis()
的小时+分钟相比,MilliSeconds
要大得多。
这是我的代码:
// setting my time in milliseconds. hour1 and minute1 taken from timepicker.
app.setTimeMillisDosageTime(TimeUnit.HOURS.toMillis(hour1) + TimeUnit.MINUTES.toMillis(minute1));
我有一个TimerTask
,我在减去System.currentTimeMillis
。
TimerTask task = new TimerTask()
{
timeInMillis = app.getTimeMillisDosageTime();
TimerTask task = new TimerTask()
{
@Override
public void run()
{
long ellapsedMillis = timeinMillis - System.currentTimeMillis();
}
timer = new Timer(false);
timer.schedule(task,0,1000);
}
}
但我的elapsedMillis
总是消极的。任何解决方案?
答案 0 :(得分:0)
TimeUnit.HOURS.toMillis
// given number is converted to milliseconds. if you give 1 as input 60*60*1000 is output.
TimeUnit.HOURS.toMillis(1) = 60* 60 * 1000
TimeUnit.MINUTES.toMillis(1) = 60 * 1000
System.currentTimeMillis() //milli seconds elapsed from January 1, 1970 UTC(coordinated universal time)
但你需要的是将这些小时和分钟设置为当前日期,然后计算从1970年开始经过的秒数。
Calendar chosenTime = Calendar.getInstance();
chosenTime.set(Calendar.HOUR_OF_DAY, selectedHour);
chosenTime.set(Calendar.MINUTE, selectedMinute);
chosenTime.getTime().get(Time) // This gives you exact seconds that can be substracted from System.currentTimeMillis.