time picker showing time like 4:7 instead of 04:07

前端 未结 8 1843
小蘑菇
小蘑菇 2021-02-05 05:41

I have a time picker function which sets time in an EditText . But the format it shows is not suitable. for example for 04:07pm is shown as 4:7. whenever the digit in time is l

8条回答
  •  滥情空心
    2021-02-05 06:20

    First need to create one function that check your input and convert it in String as per condition.

    public String pad(int input) {
        if (input >= 10) {
            return String.valueOf(input);
        } else {
            return "0" + String.valueOf(input);
        }
    }
    

    Then you can call like this

     txtTime1.setText(pad(hourOfDay) + ":" + pad(minute));
    

提交回复
热议问题