我已经制作了以mmm:ss
格式
以下是代码:
class CountDown extends CountDownTimer {
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long ms = millisUntilFinished;
String text = String.format("%03d:%02d",
TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
tvTimer.setText(text);
}
@Override
public void onFinish() {
try {
new AlertDialog.Builder(MCQActivity.this)
.setTitle("Time is over")
.setMessage("Your time is finished, Click on ok to go result page")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
saveWorkAndShowResult();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我要通过
mCountDownTimer = new CountDown(1000 * 60 * 30, 1000);
mCountDownTimer.start();
工作正常并向我显示30 mins
但如果我想展示180 min
mCountDownTimer = new CountDown(1000 * 60 * 180, 1000);
从60 min
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您的代码会减少到分钟数,不包括任何小时数。只需删除- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)
以解决此问题的位
答案 1 :(得分:0)
您可以在代码中使用以下转换功能;
Long hour = TimeUnit.HOURS.convert(millisUntilFinished, TimeUnit.MILLISECONDS);
millisUntilFinished= millisUntilFinished% (1000 * 60 * 60);
Long minutes = TimeUnit.MINUTES.convert(millisUntilFinished, TimeUnit.MILLISECONDS);
millisUntilFinished= millisUntilFinished% (1000 * 60);
Long seconds = TimeUnit.SECONDS.convert(millisUntilFinished, TimeUnit.MILLISECONDS);
millisUntilFinished= millisUntilFinished% 1000;
Long milisec = millisUntilFinished;
然后过去改变你的String格式化程序;
String text = String.format("%03d:%02d", minutes , seconds );
答案 2 :(得分:0)
var date = new Date($('#date')。val()); date.setTime(date.getTime() - date.getTimezoneOffset()* 60 * 1000);