Android DatePicker showing wrong year above calendar
<p>I'm trying to create a date picker fragment and am running into a problem where the year above the calendar isn't updating properly.</p>

<p>The first problem I have is that if I use the up/down year buttons the title of the fragment changes to "Set date" and the year above the calendar doesn't update (the day grid does).</p>

<p>The second problem is when I open it with dates well into the future or the past the dates it shows in the title and above the calendar are very wrong.  </p>

<p>Going into the future the breakpoint occurs between 2037/4/11 and 2038/4/11.  While I haven't hunted down the exact day I suspect a 32bit unix time lurking in the background is somehow involved.</p>

<p>Going into the past however the break occurs between 1902/4/11 and 1901/4/11; which doesn't align with the start of a 32bit timestamp but does correspond to the invalid dates seen when crossing into 2038.</p>

<p>This happens on both the 4.1 tablets I have for testing and in 4.1 and 4.2 AVDs.  Screenshots are cropped from tablet AVDs.</p>

<p>At this point I'm wondering if I'm doing something wrong, or have found a bug in the OS itself.</p>

<p>Initial state, loaded with today:
<img src="http://i.stack.imgur.com/ncHOk.png" alt="Initial state, loaded with today"></p>

<p>After pressing the + year button, wrong title year above calendar not updated:<br>
<img src="http://i.stack.imgur.com/V9fB0.png" alt="After pressing the + year button"></p>

<p>Loaded with a 2037 date, initial state is good:
<img src="http://i.stack.imgur.com/AWLBc.png" alt="Loaded with a 2037 date"></p>

<p>Loaded with a 2038 date, note time warps back to 1902 in the title and above the calendar:
<img src="http://i.stack.imgur.com/I7Ebl.png" alt="Loaded with a 2038 date"></p>

<p>Loaded with a 1902 date, nothing wrong:
<img src="http://i.stack.imgur.com/eprj2.png" alt="Loaded with a 1902 date"></p>

<p>Loaded with a 1901 date, note time warps forward to 2037 in the title and above the calendar:
<img src="http://i.stack.imgur.com/9ikTK.png" alt="Loaded with a 1901 date"></p>

<p>My fragment class:</p>

<pre><code>public class DatePickerFragment extends DialogFragment {
    private static int _month = 0;
    private static int _day = 0;
    private static int _year = 0;
    private static int _buttonId = 0;

    public DatePickerFragment(){
    }

    public static final DatePickerFragment newInstance(int buttonId, int year, int month, int day) {
        DatePickerFragment f = new DatePickerFragment();
        _buttonId = buttonId;
        _year = year;
        _month = month;
        _day = day;
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(getActivity(),  mDateSetListener, _year, _month, _day);
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
                    //update the control whose press opened the DatePpickerFragment
            Button _button = (Button)getActivity().findViewById (_buttonId);
            if(_button != null) {
                _button.setText(month+1 + "/" + day + "/" + year);
            }
        }
    };
}
</code></pre>

<p>And the code to install the button handler that opens the fragment:</p>

<pre><code>    Button button = (Button) findViewById(buttonID);

    button.setOnClickListener( new View.OnClickListener() {
        public void onClick(View v) {

            Button button = (Button) v;
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(df.parse(button.getText().toString()));
            } catch (ParseException e) {
                // Do nothing. Calendar defaults to the current date. Use that instead.
            }

            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            final DatePickerFragment newFragment = DatePickerFragment.newInstance(button.getId(), year, month, day);
            newFragment.show(getFragmentManager(), "");
        }
    });
}
</code></pre>
