Android: Setting time in time picker with the time

2019-01-18 04:26发布

In my app, I am showing time in text view as 07:00 PM. On click of the text view, a time picker dialog pops up, In that time picker, I have to show exactly the 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

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-18 04:56

I have created this helper function to get time

        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();
}

and How to get current time in required format, I use following code. // PARAM Date is date of time you want to format // PARAM currentFormat of date // 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 "";
    }

}

usage of getFormattedDate is:

       String date = getFormattedDate(dateYouWantToFormate,"hh:mm a","hh:mm");
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-18 04:57

If you're using the TimePicker nowdays its probably best to check the Build Version as the setCurrentHour and setCurrentMinute has been deprecated.

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());
}
查看更多
别忘想泡老子
4楼-- · 2019-01-18 04:59

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));
查看更多
走好不送
5楼-- · 2019-01-18 05:05

For AM/PM you have to implement a logic for that and that is as below.. so you can get the output as you mentioned ... just try below code...

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();
查看更多
淡お忘
6楼-- · 2019-01-18 05:21

If anybody using the following format that is without using timepicker widget use this one..

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);

    }
};
查看更多
登录 后发表回答