TimePickerDialog和AM或PM

我有一个TimePickerDialog与is24Hour设置为false,因为我想给最终用户提供更熟悉的12小时格式。 如果设置了小时,分钟和AM PM指示灯,并且返回时间,如何确定最终用户是select了AM还是PM?

这是我对听众的感受:

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; // mIsAM = WHERE CAN I GET THIS VALUE } }; 

hourOfDay将始终是24小时。 如果用is24HourView设置为false打开对话框,则用户不必处理24小时格式化的时间,但Android会在调用onTimeSet()时将其转换为24小时的时间。

这对我工作:

 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String am_pm = ""; Calendar datetime = Calendar.getInstance(); datetime.set(Calendar.HOUR_OF_DAY, hourOfDay); datetime.set(Calendar.MINUTE, minute); if (datetime.get(Calendar.AM_PM) == Calendar.AM) am_pm = "AM"; else if (datetime.get(Calendar.AM_PM) == Calendar.PM) am_pm = "PM"; String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+""; ((Button)getActivity().findViewById(R.id.btnEventStartTime)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm ); } 

嗨,你可以使用你自己的格式是这样的:

  String myFormat = "hh:mm a"; // your own format SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); String formated_time = sdf.format(calendar_time.getTime()); //format your time 

我发现“a”是代表AM-PM的格式化代码: http : //www.tutorialspoint.com/java/java_date_time.htm

我遇到了同样的问题。 我有一个TimePicker在我的应用程序中,你select了一个时间后,点击一个button,你会被带到一个显示什么时间被选中的屏幕。 select的“时间”是正确的,但有时AM / PM的价值会与所select的相反。 我最终通过改变我为TimePicker中“hrs”参数存储的内容来修复它。

 private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hrs, int mins) { hour = hrs; minute = mins; c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hour); //instead of c.set(Calendar.HOUR, hour); c.set(Calendar.MINUTE, minute); 

现在,当我按下button进入下一个屏幕时,它正确地显示了正确的AM / PM值。

用户select显示正确的HH:MM格式的烤面包信息

  public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String AM_PM = " AM"; String mm_precede = ""; if (hourOfDay >= 12) { AM_PM = " PM"; if (hourOfDay >=13 && hourOfDay < 24) { hourOfDay -= 12; } else { hourOfDay = 12; } } else if (hourOfDay == 0) { hourOfDay = 12; } if (minute < 10) { mm_precede = "0"; } Toast.makeText(mContext, "" + hourOfDay + ":" + mm_precede + minute + AM_PM, Toast.LENGTH_SHORT).show(); } 

试试这个简单的方法:

  void openTimeFragment() { TimePickerDialog mTimePicker; final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); mTimePicker = new TimePickerDialog(YourActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { String time = selectedHour + ":" + selectedMinute; SimpleDateFormat fmt = new SimpleDateFormat("HH:mm"); Date date = null; try { date = fmt.parse(time ); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat fmtOut = new SimpleDateFormat("hh:mm aa"); String formattedTime=fmtOut.format(date); edtTextTime.setText(formattedTime); } }, hour, minute, false);//No 24 hour time mTimePicker.setTitle("Select Time"); mTimePicker.show(); } 
  private String getTime(int hr,int min) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY,hr); cal.set(Calendar.MINUTE,min); Format formatter; formatter = new SimpleDateFormat("h:mm a"); return formatter.format(cal.getTime()); } 

尝试这个 :

  Calendar calendar = Calendar.getInstance(); calendar.set(0, 0, 0, hourOfDay, minute, 0); long timeInMillis = calendar.getTimeInMillis(); java.text.DateFormat dateFormatter = new SimpleDateFormat("hh:mm a"); Date date = new Date(); date.setTime(timeInMillis); time.setText(dateFormatter.format(date)); 

如果您希望直接在“时间选取器”对话框中设置上午/下午,则有一个解决办法。 如果你用当前/默认时间预先填充EditText,这将起作用。 – 在实例化对话框之前,如果EditText具有PM时间值,则将小时分量增加12。

sref_Time3是安培值; sref_Time1是小时组件。

  if (sref_Time3.equals("PM")) { hour = hour + 12; } else if (sref_Time1.equals("12")) { hour = 0; } // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); 

感谢和问候。

屏幕 时间选取器对话框

这适用于我:

 TimePickerDialog timePickerDialog = new TimePickerDialog(AddFocusActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.HOUR,hourOfDay); time = calendar.get(Calendar.HOUR) + ((calendar.get(Calendar.AM_PM) == Calendar.AM) ? "am" : "pm"); Log.e("time",time); } },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE), false); 
 public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR); int minute = c.get(Calendar.MINUTE); // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar calendar = Calendar.getInstance(); calendar.set(0, 0, 0, hourOfDay, minute); textView.setText((String) DateFormat.format("hh:mm aaa", calendar)); } } //For showing the dialog put below two lines where you want to call TimePickerFragment timePickerFragment = new TimePickerFragment(); timePickerFragment.show(getSupportFragmentManager(), "TimePicker"); 

你可以试试我的解决scheme,我用“hh:mm a”格式设置AM / PM时间

 SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a", Locale.ENGLISH); String formattedTime = formatter.format(myCalendar.getTime()); EditText.setText(formattedTime); 
 private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { boolean isPM = (hourOfDay >= 12); tvTime.setText(String.format("%02d:%02d %s", (hourOfDay == 12 || hourOfDay == 0) ? 12 : hourOfDay % 12, minute, isPM ? "PM" : "AM")); } };