如何在android中获得时间选择器的时间?

时间:2013-04-16 06:25:54

标签: android

我正在为我的应用程序创建一个调度程序,我想从时间选择器中设置时间。我面临的问题是我有时间和date.Here是代码,请建议如何执行此操作。

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    //etTime.setText(fmtTime.format(myCalendar.getTime()));
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    /*calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);*/

    if(calSet.compareTo(calNow) <= 0){
        //Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }

    setAlarm(calSet);

 }
};

3 个答案:

答案 0 :(得分:1)

你可以像这样设定时间..

 TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    textview.settext("Time:"+hourOfDay+":"+minute);


 }
};

答案 1 :(得分:0)

// int hourOfDay conatins days of day and int minute将包含minutes.use这个变量来设置时间。

public void onTimeSet(TimePicker view,int hourOfDay,int minute)

答案 2 :(得分:0)

试试这个..

String s;
Format formatter;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
calendar.set(Calendar.MINUTE, tp.setCurrentMinutes());
formatter = new SimpleDateFormat("hh:mm:ss");
s = formatter.format(calendar.getTime());