我在我的应用程序中使用CountDownTimer
,它显示错误的时间。
这就是我的尝试。
long miliSecsDate = milliseconds ("2017-11-18 12:35");
将日期和时间转换为毫秒
public long milliseconds(String date)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try
{
Date mDate = sdf.parse(date);
long timeInMilliseconds = mDate.getTime();
return timeInMilliseconds;
}
catch (ParseException e)
{
e.printStackTrace();
}
return 0;
}
倒数计时器
public void showCountdown(long miliSecsDate){
new CountDownTimer(miliSecsDate, 1000) {
public void onTick(long millisUntilFinished) {
long seconds= TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
int minutes = (int)(seconds % 3600) / 60;
int hours = (int)seconds / 3600;
seconds = seconds % 60;
Log.d("time",""+hours+" : "+minutes+" : "+seconds);
}
public void onFinish() {
txtRemainingTime.setText("done!");
}
}.start();
}
我的Log cat如下所示
D/time: 419719 : 3 : 43
答案 0 :(得分:0)
尝试使用:
String myDate = "2017-11-18 12:35";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = sdf.parse(myDate);
long millis = date.getTime();
long seconds = millis/1000;
long sec = seconds % 60;
long min = (seconds / 60) % 60;
long hrs = (seconds / (60 * 60)) % 24;
Log.d("time",""+hrs+" : "+min+" : "+sec);
如果您想使用TimeUnit
课程Refer link