0

我有一个可以正常工作的倒计时计时器,除了我得到的数字在第 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;
4

1 回答 1

0

Joda time在其Period 类中提供了一个更简单(且经过充分验证)的解决方案。像这样的东西应该可以解决问题(未经测试):

Period period = new Period(calendar1.getTimeInMillis(), calendar2.getTimeInMillis());
int days = period.getDays();
int hours = period.getHours();
... etc.
于 2012-12-15T22:28:37.963 回答