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.
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.
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.
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.
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));
Just set the text to "%02d:%02d" as follows:
e6.setText(String.format("%02d:%02d", selectedHour, selectedMinute));
Hope so this might help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With