以hh:mm格式显示时间选择器
我必须在单击编辑文本时以hh:mm格式显示时间选择器。这样我就使用了下面的代码。
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();
}
});
它运行良好并显示时间选择器。但是我的 issue 是,当我在时间选择器中选择04:15时,它将显示4:15。在4之前不包含0,我需要以hh:mm格式显示时间选择器。
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.
刚才我已经提到了这个发布并更改单行:
Just now I had referred this post and change the single line:
此行
e6.setText(selectedHour +: + selectedMinute);
e6.setText(selectedHour + ":" + selectedMinute);
至
e6.setText(String.format(%02d :%02d,selectedHour,
selectedMinute));
e6.setText(String.format("%02d:%02d", selectedHour, selectedMinute));