Search code examples
javadateformattinggregorian-calendar

Elapsed time format HH:mm:ss


It's a simple problem but I'm not getting it to work.

I'm incrementing a variable each second and setting it in a GregorianCalendar in miliseconds.

And I'm using this format HH:mmss to present the elpased time.

The problem is that the hour starts showing 01 instead of 00. For instance, after 1 minute and 35 seconds what is shown is: 01:01:35 instead of 00:01:35.

Where could be the problem?

There is the important code:

GregorianCalendar timeIntervalDone = new GregorianCalendar(TimeZone.getTimeZone("GMT-1")); //initially I didn't have the TimeZone set, but no difference
SimpleDateFormat dateTimeIntervalFormat = new SimpleDateFormat("HH:mm:ss");

public String getTimeIntervalDoneAsString() {
    timeIntervalDone.setTimeInMillis(mTimeIntervalDone); //mTimeIntervalDone is the counter: 3seccond -> mTimeIntervalDone = 3000
    return dateTimeIntervalFormat.format(timeIntervalDone.getTime());
}

Solution

  • I finally got it:

    GregorianCalendar timeIntervalDone = new GregorianCalendar(); 
    SimpleDateFormat dateTimeIntervalFormat = new SimpleDateFormat("HH:mm:ss");
    dateTimeIntervalFormat.setTimeZone(TimeZone.getTimeZone("GMT"));