codeme2020
codeme2020

Reputation: 853

Android: Time.setToNow()- Minute value not formatted correctly?

In the following code In my Android application I am sdisplaying the time as the current time like so:

 String timeToDisplay;
 Time time = new Time();
    time.setToNow();
    timeToDisplay = time.hour + ":" + time.minute + ":"
    + time.second;

This is working as intended but the output minute format is not exactly what I want.

I want to have e.g.:

21:05:45

But I am getting:

21:5:45

i.e. the zero is missing from the minute.

How can I ensure that the zero is shown in the time.minute if the minute is under 10 past the hour?

Upvotes: 0

Views: 721

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

Since you are getting the int value of minute it wont really give you the 0 before it but you can still format your string and append "0" before the time.minute

sample:

timeToDisplay = time.hour + ":" + (time.minute < 10 ? "0"+time.minute : time.minute) + ":"

Upvotes: 1

Related Questions