Search code examples
javaandroidkotlinstopwatchchronometer

How do i start chronometer from specific time in string format (01:30:24)? [Android]


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

Nb: android using Kotlin


Solution

  • 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.