Reputation: 33
My DatePickerDialog
shows current date by default. Whenever moving past year on that time same DAY
is auto selecting. Does any guys meet this issue? Any help would be appreciated.
public void datePicker(Context ctx,final CommonEditTextRegular editText) {
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog fromDatePickerDialog = new DatePickerDialog(ctx, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
String myFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
editText.setText(sdf.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePickerDialog.show();
fromDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
}
Today is 9Th May 2019. When ever checking 2018 year.. 9Th May 2018 automatic select.
Upvotes: 0
Views: 48
Reputation: 1448
Yes, this is how it works. It's not a bug. The Calendar is a Year based calendar.
When you change the year, a new whole year is generated and therefore a date is selected. by default, the same day/month as the previous one.
Upvotes: 1