Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show the time picker in hh:mm format

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.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
                    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
                    Calendar mcurrentTime = Calendar.getInstance();
                    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                    int minute = mcurrentTime.get(Calendar.MINUTE);
                    TimePickerDialog mTimePicker;
                    mTimePicker = new TimePickerDialog(AddFlight.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                            e6.setText(selectedHour + ":" + selectedMinute);

                        }
                    }, hour, minute, true);//Yes 24 hour time
                    mTimePicker.setTitle("Select Time");
                    mTimePicker.show();

            }
        });

It is working well and shows the time picker.But my issue is,when I select the 04:15 in timepicker, it display 4:15.It doesn't include 0 before 4.I need to show the timepicker in hh:mm format.

Anyone can help me with this.Thank you.

like image 940
Steve Avatar asked Sep 01 '15 09:09

Steve


People also ask

What is a time picker?

Time pickers allow users to enter a specific time value. They can be used for a wide range of scenarios. Common use cases include: Setting an alarm. Scheduling a meeting.

How can you use time picker in Android application?

Android Time Picker allows you to select the time of day in either 24 hour or AM/PM mode. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.

Which method check the current mode of time picker?

is24HourView(): This method is used to check the current mode of the time picker. This method returns true if its 24 hour mode or false if AM/PM mode is set.


2 Answers

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));

like image 85
Steve Avatar answered Oct 06 '22 00:10

Steve


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

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

Hope so this might help you

like image 38
Sandeep Manmode Avatar answered Oct 05 '22 23:10

Sandeep Manmode