在我的应用程序,我显示的时间在文本视图为19:00。在点击的文本视图,一时间选择器对话框弹出,。在这段时间选择器,我必须准确显示同一时间是什么出现在TextView中。但是,我没有得到如何做到这一点。
code
的SimpleDateFormat自卫队=新的SimpleDateFormat(HH:mm的);
// INT HR = 0;
日期日期= NULL;
尝试
{
日期= sdf.parse(resDateArray [3]);
}
赶上(ParseException的E){
e.printStackTrace();
}
最后台历挂历= Calendar.getInstance();
calendar.setTime(日期);
// TP是引用变量的时间选择器
tp.setCurrentHour(calendar.get(Calendar.HOUR));
tp.setCurrentMinute(calendar.get(Calendar.MINUTE));
}//其他
解决方案
您应该使用的SimpleDateFormat
获得日期
从 String对象
。
然后,只需拨打
picker.setCurrentHour(date.getHours())
和
picker.setCurrentMinute(date.getMinutes())
由于日期
对象德precated,你应该使用日历
代替它。
您可以实例化一个日历
这种方式:
日历C = Calendar.getInstance();
c.setTime(日期);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendat.MINUTE));
编辑:完整的code
的SimpleDateFormat自卫队=新的SimpleDateFormat(HH:SS);
日期日期= NULL;
尝试 {
日期= sdf.parse(07);
}赶上(ParseException的E){
}
日历C = Calendar.getInstance();
c.setTime(日期);
TimePicker选择器=新TimePicker(getApplicationContext());
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendar.MINUTE));
In my app, I am showing time in text view as 07:00 PM. On click of text view, a time picker dialog pops up,. In that time picker, I have to show exactly same time as what is appearing in textview. But, I am not getting how to do that.
CODE
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
解决方案
You should use a SimpleDateFormat
to get a Date
object from your String
.
Then just call
picker.setCurrentHour(date.getHours())
and
picker.setCurrentMinute(date.getMinutes())
Since the Date
object is deprecated, you should use a Calendar
instead of it.
You can instantiate a Calendar
that way :
Calendar c = Calendar.getInstance();
c.setTime(date);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendat.MINUTE));
Edit : the complete code
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));