1

I have implemented the DatePickerDialog, and it works. However, the title seems to not have a format. For example, it should read: Tuesday, April 12, 2011. But instead it reads 3, 2011 4 12.

The source code looks like it should be formatting the title, but it isn't:

private void updateTitle(int year, int month, int day) {
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, month);
    mCalendar.set(Calendar.DAY_OF_MONTH, day);
    setTitle(mTitleDateFormat.format(mCalendar.getTime()));
}
1
  • I actually found the answer to this. If I extend the DatePickerDialog, I can have more control over it and modify the title to my liking. Commented Apr 13, 2011 at 4:03

1 Answer 1

4

As you say,DatePickerDialog title format depend "mTitleDateFormat" that initialize in DatePickerDialog constructor, like this:mTitleDateFormat = java.text.DateFormat. getDateInstance(java.text.DateFormat.FULL);

But mTitleDateFormat is private ,outer can not modify .So you can extends class DatePickerDialog .

The follow code work well in my work. Class code:

class DatePickerDialog extends android.app.DatePickerDialog {
    public DatePickerDialog(Context context, OnDateSetListener callBack,
            int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        updateTitle(year, monthOfYear, dayOfMonth);
    }
    public void onDateChanged(DatePicker view, int year,
            int month, int day) {
        updateTitle(year, month, day);
    }
    private void updateTitle(int year, int month, int day) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mCalendar.set(Calendar.DAY_OF_MONTH, day);
        setTitle(getFormat().format(mCalendar.getTime()));
    }   
        /*
         * the format for dialog tile,and you can override this method
         */
    public SimpleDateFormat getFormat(){
        return new SimpleDateFormat("yyyy-MM-dd");
    };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.