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

前端 未结 8 1845
小蘑菇
小蘑菇 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条回答
  •  -上瘾入骨i
    2021-02-05 06:36

    You can check if the hours and minutes are less then ten. If so, you just add a "0" infront of that specific string. Just modify your code like this:

    @Override
        public void onTimeSet(TimePicker view, int hourOfDay,
        int minute) {
            // Display Selected time in textbox
    
            String hourString;
            if (hourOfDay < 10)
                hourString = "0" + hourOfDay;
            else
                hourString = "" +hourOfDay;
    
            String minuteSting;
            if (minute < 10)
                minuteSting = "0" + minute;
            else
                minuteSting = "" +minute;
    
            txtTime1.setText(hourString + ":" + minuteSting);
        }
    

提交回复
热议问题