-1

How to set start time in chronometer which is in string format (eg - 01:30:24)?

Nb: android using Kotlin

SARATH V
  • 500
  • 1
  • 7
  • 33

1 Answers1

1

According to the answer of this post,

How do I start chronometer with a specific starting time?

I think what you want to acheive is this.

//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)))

Hope it helps.

March3April4
  • 2,131
  • 1
  • 18
  • 37
  • I recommend you avoid the long outdated and notoriously troublesome `SimpleDateFormat` class.Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Aug 09 '19 at 12:15