在我的应用中,我在文本视图中显示时间为07:00 PM。单击文本视图时,会弹出一个时间选择器对话框,在该时间选择器中,我必须显示与textview中显示的内容完全相同的时间。但是,我没有得到如何做到这一点。
代码
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
//int hr = 0;
Date date = null;
try
{
date = sdf.parse(resDateArray[3]);
}
catch (ParseException e) {
e.printStackTrace();
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//tp is reference variable for time picker
tp.setCurrentHour(calendar.get(Calendar.HOUR));
tp.setCurrentMinute(calendar.get(Calendar.MINUTE));
}//else
答案 0 :(得分:32)
您应该使用SimpleDateFormat
从Date
获取String
个对象。
然后打电话
picker.setCurrentHour(date.getHours())
和
picker.setCurrentMinute(date.getMinutes())
由于不推荐使用Date
对象,因此应使用Calendar
代替它。
您可以通过这种方式实例化Calendar
:
Calendar c = Calendar.getInstance();
c.setTime(date);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendat.MINUTE));
编辑:完整代码
SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
Date date = null;
try {
date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);
TimePicker picker = new TimePicker(getApplicationContext());
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendar.MINUTE));
答案 1 :(得分:4)
如果使用以下格式但没有使用timepicker小部件的人使用这个..
mStartTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(AddAppointmentActivity.this, onStartTimeListener, calendar
.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), false).show();
}
});
TimePickerDialog.OnTimeSetListener onStartTimeListener = new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String AM_PM;
int am_pm;
mStartTime.setText(hourOfDay + " : " + minute + " " + AM_PM);
calendar.set(Calendar.HOUR, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
}
};
答案 2 :(得分:2)
我已经创建了这个辅助函数来获取时间
public static void showTime(final Context context, final TextView textView) {
final Calendar myCalendar = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String am_pm = "";
myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
myCalendar.set(Calendar.MINUTE, minute);
if (myCalendar.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (myCalendar.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
String strHrsToShow = (myCalendar.get(Calendar.HOUR) == 0) ? "12" : myCalendar.get(Calendar.HOUR) + "";
//UIHelper.showLongToastInCenter(context, strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);
textView.setText(strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);
}
};
new TimePickerDialog(context, mTimeSetListener, myCalendar.get(Calendar.HOUR), myCalendar.get(Calendar.MINUTE), false).show();
}
以及如何以所需格式获取当前时间,我使用以下代码。 // PARAM日期是您要格式化的日期 // PARAM currentFormat的日期 // PARAM requiredFormat of date
public static String getFormattedDate(String date, String currentFormate, String requiredFormate) {
//SimpleDateFormat formatActual = new SimpleDateFormat("yyyy - MM - dd");
if (date != null) {
SimpleDateFormat formatActual = new SimpleDateFormat(currentFormate);
Date dateA = null;
try {
dateA = formatActual.parse(date);
System.out.println(dateA);
} catch (ParseException e) {
e.printStackTrace();
}
//SimpleDateFormat formatter = new SimpleDateFormat("dd MMM, yyyy");
SimpleDateFormat formatter = new SimpleDateFormat(requiredFormate);
String format = formatter.format(dateA);
System.out.println(format);
return format;
} else {
return "";
}
}
使用getFormattedDate是:
String date = getFormattedDate(dateYouWantToFormate,"hh:mm a","hh:mm");
答案 3 :(得分:0)
对于AM / PM,你必须为它实现一个逻辑,如下所示..所以你可以得到你提到的输出...只需尝试下面的代码......
int hr=tp.getCurrentHour();
String a="AM";
if(hr>=12){
hr=hr-12;
a="PM";
}
String tm=hr+":"+tp.getCurrentMinute()+" "+a;
Toast.makeText(MainActivity.this, tm, 500).show();
答案 4 :(得分:0)
如果现在使用的是TimePicker,则最好不要使用Build版本,因为setCurrentHour和setCurrentMinute已被弃用。
SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
Date date = null;
try {
date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);
TimePicker timePicker = new TimePicker(getApplicationContext());
if (Build.VERSION.SDK_INT >= 23) {
timePicker.setHour(calculateHours());
timePicker.setMinute(calculateMinutes());
}
else {
timePicker.setCurrentHour(calculateHours());
timePicker.setCurrentMinute(calculateMinutes());
}
答案 5 :(得分:0)
sdk 23版之后有一些更改;
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = null;
try {
date = sdf.parse("12:02:32");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(date != null) {
Calendar c = Calendar.getInstance();
c.setTime(date);
sleepTimeStartPicker.setHour(c.get(Calendar.HOUR_OF_DAY));
sleepTimeStartPicker.setMinute(c.get(Calendar.MINUTE));
}
答案 6 :(得分:0)
在onTimeSet()
函数中获取时间时必须牢记一个重要点,在日历实例中没有用于设置AM_PM
的特殊函数。
当我们设置时间AM_PM
时,将自动进行相应的设置,就像如果在选择小时和分钟的同时选择了AM,则<calendarVariable>.get(Calendar.AM_PM)
将返回0,而在相反情况下(对于PM)则返回1。>