如何从特定时间以字符串格式(01:30:24)启动计时器? [Android]

时间:2019-08-08 09:32:37

标签: java android kotlin stopwatch chronometer

如何在计时器中设置字符串格式的开始时间(例如-01:30:24)?

Nb:使用Kotlin的Android

1 个答案:

答案 0 :(得分:1)

根据此帖子的答案,

How do I start chronometer with a specific starting time?

我认为您想要实现的目标是

//1. parse your input as a date object.
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date startDate = format.parse("01:30:00");

//2. feed it to a Calendar Object
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);

//3. get the hour, minute, second variable
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

//4. apply to your Chronometer class.
mChronometer.setBase(SystemClock.elapsedRealtime() - (hour * 60000 * 60 + minute * 60000 + second * 1000)))

希望有帮助。