Setting time and date to date picker and time picker in android

后端 未结 5 1578
谎友^
谎友^ 2020-12-17 07:49

I am using a date picker and time picker in my application. I want to set the date and time when the page loads. Is this possible? How so?

5条回答
  •  -上瘾入骨i
    2020-12-17 07:59

    There is a updateDate method for the DatePicker.

    Here is what I use in one of my app, not using a dialog:

    //set datePicker to position
    String date_saved = project_row.getString(
        project_row.getColumnIndex("project_startdate"));
    
    int day  = Integer.parseInt(date_saved.substring(0,2));
    int year = Integer.parseInt(date_saved.substring(5,9));
    String month = date_saved.substring(2,5);
    int month_code = getMonthInt(month);
    
    project_startdate_data = (DatePicker) findViewById(R.id.project_startdate_data);
    project_startdate_data.updateDate(year, month_code, day);
    

    where project_row is my cursor, and the date is saved in the column project_startdate like 22MAY2012

    and you need this:

    private int getMonthInt(String month) {
        if (month.equalsIgnoreCase("JAN")) return 0;
        if (month.equalsIgnoreCase("FEB")) return 1;
        if (month.equalsIgnoreCase("MAR")) return 2;
        if (month.equalsIgnoreCase("APR")) return 3;
        if (month.equalsIgnoreCase("MAY")) return 4;
        if (month.equalsIgnoreCase("JUN")) return 5;
        if (month.equalsIgnoreCase("JUL")) return 6;
        if (month.equalsIgnoreCase("AUG")) return 7;
        if (month.equalsIgnoreCase("SEP")) return 8;
        if (month.equalsIgnoreCase("OCT")) return 9;
        if (month.equalsIgnoreCase("NOV")) return 10;
        if (month.equalsIgnoreCase("DEC")) return 11;
    
        //return -1 if month code not found
        return -1;
    }
    

提交回复
热议问题