Skip to content

Instantly share code, notes, and snippets.

@eddnav
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddnav/0d02bb07f8a3582f8129 to your computer and use it in GitHub Desktop.
Save eddnav/0d02bb07f8a3582f8129 to your computer and use it in GitHub Desktop.
Date picker fragment on a dialog which takes inputs from a specific field, parses date strings according to the system's preferred locale and formats its own output too in the same locale-sensitive format.
package com.tghsistemas.ispark.fragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.EditText;
import com.tghsistemas.ispark.util.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Date picker fragment on a dialog which takes inputs from a specific field, parses date strings
* according to the system's preferred locale and formats its own output too in the same locale-sensitive
* format.
*
* @author Eduardo Naveda
*/
public class DatePickerDialogFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
/**
* Input field for the date picker.
*/
private EditText field;
/**
* Constructor for a new date picker dialog fragment.
*
* @param field Input field for the date picker.
*/
public DatePickerDialogFragment(EditText field) {
this.field = field;
}
/**
* If input {@link #field}'s content is in the preferred locale format, the date picker
* parses the string and uses the date as default, else it uses today's date.
*
* @param savedInstanceState bundle containing saved state.
* @return Date picker dialog.
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String inputDateString = field.getText().toString();
Calendar calendar = Calendar.getInstance();
Date today = new Date();
try {
Date inputDate = android.text.format.DateFormat.getDateFormat(getActivity()).parse(inputDateString);
calendar.setTime(inputDate);
return new DatePickerDialog(getActivity(), this,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
calendar.setTime(today);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month,
day);
}
}
/**
* Sets the date to the input {@link #field}.
*/
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, 0, 0, 0);
field.setText(android.text.format.DateFormat.getDateFormat(getActivity()).format(calendar.getTime()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment