package android.preference;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;

public class DatePreference extends DialogPreference implements DatePicker.OnDateChangedListener {
  protected String defaultValue;
  protected String changedValue;
 
  public DatePreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  
  public DatePreference(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  
  @Override
  protected View onCreateDialogView() {
    DatePicker datePicker = new DatePicker(getContext());
    Calendar calendar = getDate();
    datePicker.init(calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH),
        this);
    return datePicker;
  }
  
  public Calendar getDate() {
    if (defaultValue == null) {
      this.defaultValue = "1970.01.01";
    }
    try {
      Date date = formatter().parse(this.defaultValue);
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      return cal;
    } catch (java.text.ParseException e) {
      e.printStackTrace();
      return new GregorianCalendar(1970, 0, 1);
    }
  }
  
  private SimpleDateFormat formatter() {
    return new SimpleDateFormat("yyyy.MM.dd");
  }
  
  @Override
  protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getString(index);
  }
  
  @Override
  protected void onSetInitialValue(boolean restoreValue, Object def) {
      if (restoreValue) {
          this.defaultValue = getPersistedString(this.defaultValue);
      } else {
          String value = (String) def;
          this.defaultValue = value;
          persistString(value);
      }
  }
  
  public void onDateChanged(DatePicker view, int year, int month, int day) {
    Calendar selected = new GregorianCalendar(year, month, day);
    this.changedValue = formatter().format(selected.getTime());
  }
  
  @Override
  protected void onDialogClosed(boolean shouldSave) {
    if (shouldSave) {
      this.defaultValue = this.changedValue;
    }
  }
}
@@ -1051,86 +1051,8 @@
 ) %7B%0A
-    if (defaultValue == null) %7B%0A      this.defaultValue = %221970.01.01%22;%0A    %7D%0A
     
@@ -1057,16 +1057,16 @@
   try %7B%0A
+
       Da
@@ -1288,40 +1288,20 @@
 urn 
-new GregorianCalendar(1970, 0, 1
+defaultDate(
 );%0A 
@@ -1679,21 +1679,16 @@
 dString(
-this.
 defaultV
@@ -1683,32 +1683,34 @@
 ing(defaultValue
+()
 );%0A      %7D else 
@@ -2176,23 +2176,23 @@
 angedValue;%0A
-
     %7D%0A  %7D%0A%7D
@@ -2190,9 +2190,282 @@
   %7D%0A  %7D%0A
+  %0A  private Calendar defaultDate() %7B%0A    return new GregorianCalendar(1970, 0, 1);%0A  %7D%0A  %0A  private String defaultValue() %7B%0A    if (this.defaultValue == null) %7B%0A      this.defaultValue = formatter().format(defaultDate().getTime());%0A    %7D%0A    return this.defaultValue;%0A  %7D%0A
 %7D

Refactor per mhoran's suggestion