Android TimePickerDialog 和 AM 或 PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2659954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TimePickerDialog and AM or PM
提问by JDM
I have a TimePickerDialog with is24Hour set to false since I want to present the end-user with the more familiar 12 hour format. When the hour, minute and AM PM indicator are set and the time is returned how can I identify whether the end-user has selected AM or PM?
我有一个将 is24Hour 设置为 false 的 TimePickerDialog,因为我想向最终用户展示更熟悉的 12 小时格式。当设置了小时、分钟和 AM PM 指示器并返回时间时,如何识别最终用户是选择了 AM 还是 PM?
This is what I have for the listener:
这是我对听众的看法:
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
}
};
回答by CommonsWare
The hourOfDay
will always be 24-hour. If you opened the dialog with is24HourView
set to false
, the user will not have to deal with 24-hour formatted times, but Android will convert that to a 24-hour time when it calls onTimeSet()
.
该hourOfDay
永远是24小时。如果您将对话框is24HourView
设置为false
,则用户将不必处理 24 小时格式的时间,但 Android 会在调用 时将其转换为 24 小时时间onTimeSet()
。
回答by Adil Malik
This worked for me:
这对我有用:
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 );
}
回答by joelreeves
I was running into the same problem. I have a TimePicker in my app where after you choose a time and hit a button you'll be taken to a screen that showed what time was chosen. The "time" chosen was correct but sometimes the AM/PM value would be opposite of what was chosen. I finally fixed it by changing what I was storing for the "hrs" argument from the TimePicker.
我遇到了同样的问题。我的应用程序中有一个 TimePicker,在您选择一个时间并点击一个按钮后,您将被带到一个显示所选时间的屏幕。选择的“时间”是正确的,但有时 AM/PM 值会与选择的相反。我最终通过更改我为 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);
Now when I hit the button to go to the next screen it properly showed the correct AM/PM value chosen.
现在,当我点击按钮进入下一个屏幕时,它正确显示了所选的正确 AM/PM 值。
回答by AmirG
Hi you could use your own format like this :
嗨,您可以像这样使用自己的格式:
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
I found that "a" is the code in formating time that represents AM-PM: http://www.tutorialspoint.com/java/java_date_time.htm
我发现“a”是代表AM-PM的格式化时间代码:http: //www.tutorialspoint.com/java/java_date_time.htm
回答by David Prun
a neat toast message with what user selected showing proper HH:MM format
一个整洁的吐司消息,用户选择显示正确的 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();
}
回答by Neha Tyagi
Try this simple method:
试试这个简单的方法:
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();
}
回答by Ronny Shibley
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());
}
回答by Lay Leangsros
Try this :
尝试这个 :
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));
回答by CodeBulls Inc.
If your expectation was to directly set AM/PM in the Time Picker Dialog box, there is a work around. This works if you pre-populate the EditText with current/default time. - Before instantiating the dialog, increase the hour component by 12 if EditText has PM time value.
如果您希望在“时间选择器”对话框中直接设置 AM/PM,则有一个变通方法。如果您使用当前/默认时间预先填充 EditText,这将起作用。- 在实例化对话框之前,如果 EditText 具有 PM 时间值,则将小时组件增加 12。
sref_Time3 is ampm value; sref_Time1 is hour component.
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()));
Thanks & Regards.
感谢和问候。
回答by Mikey Yang
This works for me :
这对我有用:
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);