Show the time picker in hh:mm format

后端 未结 4 1080
醉酒成梦
醉酒成梦 2021-01-05 07:06

I have to show the time picker in hh:mm format on Click the edit text.So that I had used the below code.

MainActivity.java:

  e6.set         


        
相关标签:
4条回答
  • 2021-01-05 07:15

    You can Try this way...

    mTimePicker = new TimePickerDialog(AddFlight.this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
            String formattedTime = "";
            String sHour = "00";
            // converting hour to tow digit if its between 0 to 9. (e.g. 7 to 07)
            if(selectedHour < 10)
                sHour = "0"+selectedHour;
            else
                sHour = String.valueOf(selectedHour);
    
    
            e6.setText(sHour + ":" + selectedMinute);
        }
    }, hour, minute, true);
    
    0 讨论(0)
  • 2021-01-05 07:19

    Just set the text to "%02d:%02d" as follows:

    e6.setText(String.format("%02d:%02d", selectedHour, selectedMinute));
    

    Hope so this might help you

    0 讨论(0)
  • 2021-01-05 07:27

    Just now I had referred this post and change the single line:

    this line

    e6.setText(selectedHour + ":" + selectedMinute);

    to

    e6.setText(String.format("%02d:%02d", selectedHour, selectedMinute));

    0 讨论(0)
  • 2021-01-05 07:29

    Just use this method,

    private String getDD(int num) {
       return num > 9 ? "" + num : "0" + num;
    }
    
    0 讨论(0)
提交回复
热议问题