Android 时间选择器格式
Android Time Picker Format
我正在使用 TimePicker
并且在选择器中它以 24 hour
格式显示时间,这很好,但是当我 select
...
获取 - 9:5 PM
要求 1 - 09:05 PM
要求 2 - 21:05
case DIALOG_TIME:
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(this, lisTime, hour, minute,
DateFormat.is24HourFormat(FormActivity.this));
.......
TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
String hour = "";
String meridiem = "";
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);
if (datetime.get(Calendar.AM_PM) == Calendar.AM)
meridiem = "AM";
else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
meridiem = "PM";
hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR));
editTime.setText(hour + ":" + String.valueOf(minute) + " " + meridiem);
}
};
所以我必须在我的代码中进行更改才能完成此操作!
对于要求 1:使用以下代码:
String date="9:5 PM";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
try {
Log.e("",""+new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse(date)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
对于要求 2:使用以下代码:
private String pad(int value){
if(value<10){
return "0"+value;
}
return ""+value;
}
已编辑:
替换下面的行
editTime.setText(hour + ":" + String.valueOf(minute) + " " + meridiem);
到
String date=pad(Integer.parseInt(hour)) + ":" + pad(minute) + " " + meridiem;
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
try {
editTime.setText(""+new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse(date)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我正在使用 TimePicker
并且在选择器中它以 24 hour
格式显示时间,这很好,但是当我 select
...
获取 - 9:5 PM
要求 1 - 09:05 PM
要求 2 - 21:05
case DIALOG_TIME:
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(this, lisTime, hour, minute,
DateFormat.is24HourFormat(FormActivity.this));
.......
TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
String hour = "";
String meridiem = "";
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);
if (datetime.get(Calendar.AM_PM) == Calendar.AM)
meridiem = "AM";
else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
meridiem = "PM";
hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR));
editTime.setText(hour + ":" + String.valueOf(minute) + " " + meridiem);
}
};
所以我必须在我的代码中进行更改才能完成此操作!
对于要求 1:使用以下代码:
String date="9:5 PM";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
try {
Log.e("",""+new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse(date)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
对于要求 2:使用以下代码:
private String pad(int value){
if(value<10){
return "0"+value;
}
return ""+value;
}
已编辑:
替换下面的行
editTime.setText(hour + ":" + String.valueOf(minute) + " " + meridiem);
到
String date=pad(Integer.parseInt(hour)) + ":" + pad(minute) + " " + meridiem;
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
try {
editTime.setText(""+new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse(date)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}