Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Setting time in time picker with the time shown in text view

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
like image 818
Nitish Avatar asked Sep 19 '12 11:09

Nitish


People also ask

How do you set the time on a time picker?

To provide a widget for selecting a time, use the Time Picker widget , you to select the time of day in either 24 hour or AM/PM mode. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.

Which method is used to set the time picker in 12 hour format?

The time can be selected by hour, minute, and AM/PM picker columns. The AM/PM mode is determined by either explicitly setting the current mode through setIs24Hour(boolean) or the widget attribute is24HourFormat (true for 24-hour mode, false for 12-hour mode).

Which method check the current mode of time picker?

In order to get the time selected by the user on the screen, you will use getCurrentHour() and getCurrentMinute() method of the TimePicker Class.

What is the method of time and date picker?

Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components. In this tutorial, we are going to demonstrate the use of Date Picker through DatePickerDialog.


1 Answers

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 this 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:

import java.util.Date;
import java.text.SimpleDateFormat; //Don't use "android.icu.text.SimpleDateFormat"
import java.text.ParseException; //Don't use "android.net.ParseException"

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
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));
like image 53
abecker Avatar answered Oct 04 '22 09:10

abecker