我有一个倒数计时器,我想在几小时,几分钟和几秒钟内打印。我有以下代码,但我得到的输出没有在几分钟内正确转换我不明白我哪里出错了。
enter code here
CountDownTimer countDownTimer = new CountDownTimer(millisset3, 1000)
{
TextView tv = (TextView) context.findViewById(R.id.textView2);
public void onFinish() {
tv.setText("Time finished!"); }
public void onTick(long millisUntilFinished) {
tv.setText("Time left for Isha: " +String.format(" %d hours, %d min, %d sec",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished) -
TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours( millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds( millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished))));
}
}.start();
}
答案 0 :(得分:0)
试试这个
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
//on button click(timer start)
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
//=====
//on button click (timer stop)
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
//==================
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
Log.e("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
答案 1 :(得分:0)
您可以将毫秒传递给此函数,您将获得格式的字符串。
public static String getFormat(long millis)
{
DateFormat outFormat = new SimpleDateFormat("hh:mm:ss");
outFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = new Date(millis);
String result = outFormat.format(d);
return result;
}