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???
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()
I use this:
/**
*
* @param datePicker
* @return a java.util.Date
*/
public static java.util.Date getDateFromDatePicket(DatePicker datePicker){
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar.getTime();
}
date=(EditText)findViewById(R.id.date_of_birth_textfield);
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
protected Dialog onCreateDialog(int id) {
switch(id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null
}
private DatePickerDialog.OnDateSetListener mDateSetListener= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
System.out.println("calendar view shown.."+view.getCalendarViewShown());
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
// mMonth = monthOfYear;
// mDay = dayOfMonth;
}
};
private void updateDisplay() {
date.setText(
new StringBuilder()
// Month is 0 `based` so add 1
.append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(""));
//date.setText(fmtDateAndTime.format(c.getTime()));
}
I'd suggest looking at the DateFormat class in android. Be careful to import the DateFormat mentioned in the android docs. After that, whatever formatting you want to achieve is really simple. For a basic Aug 12, 2012 you can do this.
DatePicker myDatePicker = (DatePicker) findViewById(R.id.mydatepicker);
String selectedDate = DateFormat.getDateInstance().format(myDatePicker.getCalendarView().getDate());
Look at the DateFormat class to get various other configurations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With