我在TextView中有时间,如10:53 PM
。单击TextView时,我在TimePickerDialog
上显示TextView时间,但我无法根据TextView显示AM / PM。对于这个要求,我看到了link1,link2,link3等等,但没有为我工作。任何人都知道这个
我的代码
String inputTime = "10:53 PM", inputHours, inputMinutes;
inputHours = inputTime.substring(0, 2);
inputMinutes = inputTime.substring(3, 5);
tvTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimePickerDialog mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
if (selectedHour == 0) {
selectedHour += 12;
timeFormat = "AM";
} else if (selectedHour == 12) {
timeFormat = "PM";
} else if (selectedHour > 12) {
selectedHour -= 12;
timeFormat = "PM";
} else {
timeFormat = "AM";
}
String selectedTime = selectedHour + ":" + selectedMinute + " " + timeFormat;
tvTime.setText(selectedTime);
}
}, Integer.parseInt(inputHours), Integer.parseInt(inputMinutes), false);//mention true for 24 hour's time format
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
答案 0 :(得分:2)
您不需要获取am / pm值因为小时将始终为24h格式。 Timepicker显示ap / pm但返回24h格式值。
这里的HourOfDay总是24小时。如果您打开is24HourView设置为false的对话框,则用户不必处理24小时格式化时间,但Android会在调用onTimeSet()时将其转换为24小时。
Calendar calendar = Calendar.getInstance();
TimePickerDialog timePickerDialogfrom = new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
//Conversion for AM/PM as you are doing
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), false);
答案 1 :(得分:0)
尝试按timepicker.setCurrentHour(Calendar.HOUR_OF_DAY);
答案 2 :(得分:0)
selectedHour
将始终为24小时格式。因此,您可以通过以下方式将其转换为12 Hour AM-PM
。
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
String AM_PM = " AM";
String mm_precede = "";
if (selectedHour >= 12) {
AM_PM = " PM";
if (selectedHour >=13 && selectedHour < 24) {
selectedHour -= 12;
}
else {
selectedHour = 12;
}
} else if (selectedHour == 0) {
selectedHour = 12;
}
if (selectedMinute < 10) {
mm_precede = "0";
}
Toast.makeText(mContext, "" + selectedHour + ":" + mm_precede + selectedMinute + AM_PM, Toast.LENGTH_SHORT).show();
}
答案 3 :(得分:0)
感谢“Ready Android”
正如“Ready Android”所述,我们必须将12小时输入时间转换为24小时格式,然后我们必须在TimePicker中传递此值,最后当我们使用所选时间时,我们必须转换24小时时间进入12小时,如此代码所示
String inputTime = "01:15 PM"
try {
// Converting 12 hrs time to 24 hrs
DateFormat inputFormat = new SimpleDateFormat("hh:mm aa", Locale.US);
DateFormat outputFormat = new SimpleDateFormat("HH:mm", Locale.US);
Log.e("finalDate", outputFormat.format(inputFormat.parse(inputTime)));
inputTime = outputFormat.format(inputFormat.parse(inputTime));
//Getting hours and minutes separately
inputHours = inputTime.substring(0, 2);
inputMinutes = inputTime.substring(3, 5);
Log.e("inputHours", inputHours);
Log.e("inputMinutes", inputMinutes);
} catch (ParseException e) {
e.printStackTrace();
}
TimePickerDialog mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
// Converting selected 24 hrs time to 12 hrs
String timeFormat;
if (selectedHour == 0) {
selectedHour += 12;
timeFormat = "AM";
} else if (selectedHour == 12) {
timeFormat = "PM";
} else if (selectedHour > 12) {
selectedHour -= 12;
timeFormat = "PM";
} else {
timeFormat = "AM";
}
// Placing the 0 in front of single digit hours and minutes
String finalSelectedHour, finalSelectedMinute;
if(selectedHour < 10)
finalSelectedHour = "0" + selectedHour;
else
finalSelectedHour = String.valueOf(selectedHour);
if (selectedMinute < 10)
finalSelectedMinute = "0" + selectedMinute;
else
finalSelectedMinute = String.valueOf(selectedMinute);
String selectedTime = finalSelectedHour + ":" + finalSelectedMinute + " " + timeFormat;
tvTime.setText(selectedTime);
}
}, Integer.parseInt(inputHours), Integer.parseInt(inputMinutes), false);//mention true for 24 hour's time format
mTimePicker.setTitle("Select Time");
mTimePicker.show();
答案 4 :(得分:0)
scheduleTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String choosedHour = "";
String choosedMinute = "";
String choosedTimeZone = "";
if(selectedHour > 12){
choosedTimeZone = "PM";
selectedHour = selectedHour - 12;
if(selectedHour < 10){
choosedHour = "0"+selectedHour;
}else{
choosedHour = ""+selectedHour;
}
}else{
choosedTimeZone = "AM";
if(selectedHour < 10){
choosedHour = "0"+selectedHour;
}else{
choosedHour = ""+selectedHour;
}
}
if(selectedMinute < 10){
choosedMinute = "0"+selectedMinute;
}else{
choosedMinute= ""+selectedMinute;
}
scheduleTime.setText(choosedHour + ":" + choosedMinute +" "+choosedTimeZone);
//scheduleTime.setText(selectedHour + ":" + selectedMinute);
//scheduleTime.setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );
}
}, hour, minute+30, false);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
我用这个逻辑来解决这个问题。