我目前正在使用TimePickerDialog设置时间并将其显示为编辑文本。但是,我遇到了格式化问题。
我想以HH:MM格式设置时间,但它设置为H:mm格式 我想要这个01:33但是我的代码给了我1:33的输出。
以下是代码: -
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
String am_pm = "";
Calendar datetime1 = Calendar.getInstance();
datetime1.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime1.set(Calendar.MINUTE, minute);
if (datetime1.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (datetime1.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
String strHrsToShow1 = (datetime1.get(Calendar.HOUR) == 00)?"12":datetime1.get(Calendar.HOUR)+"";
((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow1+":"+datetime1.get(Calendar.MINUTE)+" "+am_pm);
}
答案 0 :(得分:1)
把这一行:
txtTime1.setText(String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE)));
而不是:
((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow1+":"+datetime1.get(Calendar.MINUTE)+" "+am_pm);