Skip to content

Instantly share code, notes, and snippets.

@dleonett
Last active September 27, 2016 01:25
Show Gist options
  • Save dleonett/1eec0bda864eabcb3134e614c50133d5 to your computer and use it in GitHub Desktop.
Save dleonett/1eec0bda864eabcb3134e614c50133d5 to your computer and use it in GitHub Desktop.
Simple date picker dialog implementation
public class DateUtils {
private static final String TAG = DateUtils.class.getSimpleName();
public static String toddmmYYYY(Date date) {
if (date != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
return dateFormat.format(date);
}
return null;
}
public static String toYYYYmmdd(Date date) {
if (date != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}
return null;
}
}
public class TestActivity extends AppCompatActivity {
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
private final static String TAG = TestActivity.class.getSimpleName();
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// Fields
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
private Calendar mCalendar;
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// Methods
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mCalendar = Calendar.getInstance();
}
public void showDateDialog(View view) {
DatePickerDialog mDatePicker =
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker,
int selectedYear,
int selectedMonth,
int selectedDay) {
mCalendar.set(Calendar.YEAR, selectedYear);
mCalendar.set(Calendar.MONTH, selectedMonth);
mCalendar.set(Calendar.DAY_OF_MONTH, selectedDay);
mEditText.setText(DateUtils.toddmmYYYY(mCalendar.getTime()));
}
}, mCalendar.get(Calendar.YEAR),
mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
mDatePicker.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment