Casting and getting values from date picker and time picker in android

Rahul Kalidindi picture Rahul Kalidindi · Apr 7, 2010 · Viewed 89.6k times · Source

I have a DatePicker and a TimePicker in my app. Can anyone tell me how to get the values of the date and time that are selected??? What i mean to say is, for EditText we can declare as

final EditText name = (EditText) this.findViewById(R.id.nametext);

and we can cast the data of it by using name.getText().toString().

So similarly how can we get the values of DatePicker and TimePicker to a String???

Answer

Jim Blackler picture Jim Blackler · Apr 7, 2010

DatePicker has

getYear() 
getMonth() 
getDayOfMonth()

to make a Date object from a DatePicker you'd do new Date(datePicker.getYear() - 1900, datePicker.getMonth(), datePicker.getDayOfMonth());

Note that this constructor was deprecated in API level 1 - please refer to andrescanavesi's answer for a more appropriate way of creating a Date object.

TimePicker has

getCurrentHour()
getCurrentMinute()