我有一个倒数计时器工作正常,除了我在100秒内输入的数字超过2位数。我正在创建2个日历对象,在毫秒时间内输入,然后减去它。代码
long milsecs1= calendar1.getTimeInMillis();
long milsecs2 = calendar2.getTimeInMillis();
long diff = milsecs2 - milsecs1;
long dsecs = diff / 1000;
long ddays = diff / (24 * 60 * 60 * 1000);
diff=diff-ddays *(24 * 60 * 60 * 1000);
textDays.setText( Integer.toString( (int)ddays)+":" );
long dhours = diff / (60 * 60 * 1000);
diff=diff-dhours* (60 * 60 * 1000);
textHours.setText( Integer.toString( (int)dhours)+":" );
long dminutes = diff / (60 * 1000);
diff=diff-dminutes* (60 * 1000);
textMinuts.setText( Integer.toString( (int)dminutes)+":" );
/////////////////////////////////////////////////
// THIS IS THE PART THAT IS NOT WORKING, I WANT NUMBERS 0-99, BUT GETTING NUMBERS LIKE 230
long dseconds = diff / (100);
textSeconds.setText( Integer.toString( (int)dseconds)+":" );
diff=diff-dseconds;
答案 0 :(得分:0)
Joda time在其Period class中提供了一个更简单(并且经过彻底验证)的解决方案。这样的事情应该可以做到(未经测试):
Period period = new Period(calendar1.getTimeInMillis(), calendar2.getTimeInMillis());
int days = period.getDays();
int hours = period.getHours();
... etc.