Initialize date in android datepicker to a specific date, that is not the current date.

你离开我真会死。 提交于 2019-12-05 02:33:30

U can use the updateDate(year, month, day_of_month); date picker returns integer values of day, month and year. so the parameters must be integer values. and the integer value for the month jan in the date picker is 0.

i needed to put the date extracted from a database into a datepicker. I wrote the following code and it works.

DatePicker DOB;
SQLiteDatabase db;

DOB=(DatePicker)findViewById(R.id.datePicker1);

db = openOrCreateDatabase("BdaySMS", SQLiteDatabase.CREATE_IF_NECESSARY, null);
Cursor cur = db.rawQuery("select * from BdaySMS where ph='"+pn+"';", null);//pn is the phone no.

if(cur.moveToFirst())
{
    name.setText(cur.getString(0));
    phone.setText(cur.getString(1));
    DOB.updateDate(Integer.parseInt(cur.getString(4)),Integer.parseInt(cur.getString(3)),Integer.parseInt(cur.getString(2)));
    message.setText(cur.getString(5));
}

Use JodaTime

Here's a simple example of how I set a DatePicker and TimePicker from a DateTime object, which could be the current date or any date from the past or future (the attribute in this case is called inspected_at):

DatePicker dp = (DatePicker) findViewById(R.id.inspected_at_date);
TimePicker tp = (TimePicker) findViewById(R.id.inspected_at_time);

DateTime inspected_at = DateTime.now().minusYears(1); // Typically pulled from DB.

int year    = inspected_at.getYear() ;
int month   = inspected_at.getMonthOfYear() - 1;      // Need to subtract 1 here.
int day     = inspected_at.getDayOfMonth();  
int hour    = inspected_at.getHourOfDay();
int minutes = inspected_at.getMinuteOfHour();

dp.updateDate(year, month, day);
tp.setCurrentHour(hour);
tp.setCurrentMinute(minutes);

Hope that helps.

JP

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!