如何使用datepicker将当前日期更新为所选日期并将其保存到数据库中

时间:2015-11-18 08:09:25

标签: java android

以下代码是从日期选择器中提取日期,但不存储在数据库中。我希望将当前日期替换为自定义日期!!

private DatePickerDialog.OnDateSetListener reservationDate = new 
    DatePickerDialog.OnDateSetListener()
{ 
    @Override
    public void onDateSet(DatePicker view, int year, int month, int day){
        //Button cal=(Button)findViewById(R.id.calendarButton);
        final Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR), curMonth = c.get(Calendar.MONTH)+1, curDay = c.get(Calendar.DAY_OF_MONTH);
        dateG=(Button)findViewById(R.id.btnRem);
        //Picks the selected date, month & year & displays on button
        if((year>curYear)||(year==curYear && month+1>curMonth)||(year==curYear && month+1==curMonth && day>curDay)) {
            dayG = Integer.toString(day);
            monthG = Integer.toString(month + 1);
            yearG = Integer.toString(year);
            dateG.setText(Integer.toString(day) + "/" + Integer.    toString(month + 1) + "/" + Integer.toString(year));
            date=dayG+"/"+monthG+"/"+yearG;
            daOdb.insertDate(date);
            Toast.makeText(getBaseContext(), "Your reminder is set from " + curDay + "-" + curMonth + "-" + curYear + " to " + day + "-" + (month + 1) + "-" + year + ".", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getBaseContext(), "Please choose date after " + curDay + "-" + curMonth + "-" + curYear, Toast.LENGTH_SHORT).show();
        }
    }
};

DAOdb是insertDate()的数据库,如下所示:

public long insertDate(String date)
{
    ContentValues cv = new ContentValues();
    cv.put(DBhelper.COLUMN_DATETIME,date);
    return database.insert(DBhelper.TABLE_NAME, null, cv);
}

0 个答案:

没有答案