您好我正在使用TimePickerDialog来设置时间并将其显示到文本视图中。但是,我遇到了格式问题。这是我的代码
TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if ((hourOfDay >= 1) && (hourOfDay <= 11.59)) {
hours = hourOfDay;
am_pm = "AM";
} else if (hourOfDay >= 12) {
hours = hourOfDay - 12;
am_pm = "PM";
} else if (hourOfDay == 0) {
hours = 12;
am_pm = "AM";
}
tvScheduleTime.setText(hours + " : " + minute + " " + am_pm);
}
};
此方法适用于检索AM和PM,但编号很奇怪,例如6:08 AM
变为6:8 AM
。我怎样才能创建文本HH-MM AM / PM?
答案 0 :(得分:1)
在这种情况下,请在打印前添加此行
if(hours <10) hours ="0"+ hours;
if(minute<10)minute="0"+minute;