DatePicker Dialog – Present And Past Calendar

Date: Posted by: Karthick In: Android

Create calendar from present date:

Use it for date of birth

past_calendar

int year = myCalendar.get(Calendar.YEAR);
int month = myCalendar.get(Calendar.MONTH);
int dayofmonth = myCalendar.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog.OnDateSetListener ddate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dob.setText(sdf.format(myCalendar.getTime()));
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(
AddClient.this, R.style.Theme_AppCompat_Light_Dialog,
ddate,
year,
month,
dayofmonth
);
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();

Create calendar from past date:

Use it for feature date selection

int year1 = myCalendar1.get(Calendar.YEAR);
int month1 = myCalendar1.get(Calendar.MONTH);
int dayofmonth1 = myCalendar1.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog.OnDateSetListener ddate1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar1.set(Calendar.YEAR, year);
myCalendar1.set(Calendar.MONTH, monthOfYear);
myCalendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
delivery_date.setText(sdf.format(myCalendar1.getTime()));
}
};
DatePickerDialog datePickerDialog1 = new DatePickerDialog(AddClient.this,
R.style.Theme_AppCompat_Light_Dialog,
ddate1,
year1,
month1,
dayofmonth1
);
datePickerDialog1.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog1.show();
 

383 Views

Top