javaandroidandroid-studiotimepickerandroid-timepicker

ANDROID Time picker how to display two numbers in hour and minute


Hi everyone i would like to display when i click a button, two number in hour and minute.

my code is:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int hour= timePicker.getCurrentHour();
        int minute=timePicker.getCurrentMinute();

        Toast.makeText(MainActivity.this,"CLOCK--> "+ String.valueOf(hour)+":"+String.valueOf(minute),Toast.LENGTH_LONG).show();

    }
});       

But the output is for example "9:8".

I want display "09:08".

Sorry for my english =)


Solution

  • Try this
    Toast.makeText(MainActivity.this,"CLOCK--> "+ String.format("%02d:%02d", hour, minute),Toast.LENGTH_LONG).show();