'Android Studio - Datepicker not showing calendar from current year
in my Android Project i have a DatePicker which directly starts from year 1900.
Here is the source code:-
private void selectDate()
{
dpd = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
c.set(year, monthOfYear, dayOfMonth);
selDate=year+"/"+monthOfYear+"/"+dayOfMonth;
Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
}
}, mYear, mMonth, mDay);
dpd.show();
}
Here is a screenshot of the same:-
As you can see the calendar starts from 1900 but i want it to start from the current date
Also, you can see '2' written on the top left corner, on clicking '2' i can change the year to whatever i want to; but I don't want the '2' symbol in my Date Picker/ how i should i change the name '2' to 'Change year' text?
What should be done? Where do i need to incorporate the necessary changes?
Thanks in advance.
I have edited my code as follows and now i am getting the current date i.e oct 2016
private void selectDate()
{
dpd = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
selDate=year+"/"+monthOfYear+"/"+dayOfMonth;
Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
dpd.show();
}
And here is the updated screenshot:
Solution 1:[1]
Try this :
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
Solution 2:[2]
You need to pass the current date as an argument when invoking the DatePickerDialog
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
As per your code, I don't see where you've assigned the values for mYear, mMonth and mDate.
Solution 3:[3]
I hope this piece of snippet works for u..
String strThatDay = datePicker.getYear() + "/" + (datePicker.getMonth() + 1) + "/" + datePicker.getDayOfMonth();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date choosenDate = null;
try {
choosenDate = formatter.parse(strThatDay);//catch exception
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Calendar todayCal = Calendar.getInstance();
todayCal.setTime(choosenDate);
Solution 4:[4]
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog =
new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dialog.show();
you can initialize your picker this way. Check this on
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Krunal Pujara |
| Solution 2 | PsyGik |
| Solution 3 | PraKi |
| Solution 4 | Glorfindel |


