This question already has answers here:
Android datepicker min max date before api level 11
(7 answers)
Closed 9 years ago.
I want to set date between 1990 and present date in date picker dialog in versions below honey comb,it is possible for versions above honey comb using functions setMinDate() and setMaxDate(),I tried the following code and it is possible to set either mindate or max date for lower versions,,the code is
calendar = new GregorianCalendar();
mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
DatePicker datePicker = (DatePicker) mDatePickerField.get(dialog);
final int maxYear = calendar.get(Calendar.YEAR);
final int maxMonth = calendar.get(Calendar.MONTH);
final int maxDay = calendar.get(Calendar.DAY_OF_MONTH);
datePicker.init(maxYear, maxMonth, maxDay,
new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, day);
if (newDate.after(calendar)) {
view.init(maxYear, maxMonth, maxDay, this);
}
}
});
Log.w(TAG, "API Level < 11 so not restricting date range...");
Please give me some new code or change the existing code for datepicker supporting both mindate and maxdate,,,,,thanks
I think you should use update instead of init.
replace this line
view.init(maxYear, maxMonth, maxDay, this);
with
view.updateDate(maxYear, maxMonth, maxDay);
Related
I try to call AlertDialog on a certain date every year when launching an app that day that is defined but something fails and I ask for your help. Note, for example, on November 11 of each year, it is necessary to run AlertDialog all day when launching the app, and when November 12 start, AlertDialog will not be displayed until November 11 of the next year. Thanks in advance.
Calendar start = Calendar.getInstance();
start.set(Calendar.YEAR, Calendar.NOVEMBER, 11);
Date today = start.getTime();
if(start.equals(today)){
showStartDialog();
}
You can try
Calendar start = Calendar.getInstance();
Date today = start.getTime();
start.set(calendar.get(Calendar.YEAR), Calendar.NOVEMBER, 11)
if(start.getTime().equals(today)){
showStartDialog();
}
Or you can just
Calendar start = Calendar.getInstance();
int month = start.get(Calendar.MONTH)
int dayOfMonth = start.get(Calendar.DAY_OF_MONTH)
if (month == Calendar.November && dayOfMonth == 11) { ...}
Hi I implemented a DatePickerDialog but I want to set range, I found setMinDate and setMaxDate, but I don't know how to put values. I want to set the calendar for a user of min age 18 and max age 100; I think it's about calculations into milliseconds.
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, year, month, day);
dpd.getDatePicker().setMaxDate(max);
return dpd;
I don't know what value I have to put to max to do 2016 - 18.
Use this code, to set maximum date and minimum date from current date.
Calendar minCal = Calendar.getInstance();
minCal.set(Calendar.YEAR, minCal.get(Calendar.YEAR) - 100);
Calendar maxCal = Calendar.getInstance();
maxCal.set(Calendar.YEAR, maxCal.get(Calendar.YEAR) - 18);
dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
dialog.getDatePicker().setMaxDate(maxCal.getTimeInMillis());
I guess this is what you need:
final Calendar maxc = Calendar.getInstance();
int year = maxc.get(Calendar.YEAR);
maxc.set(Calendar.YEAR,year-18);
datePickerDialog.getDatePicker().setMaxDate(maxc.getTimeInMillis());
final Calendar minc = Calendar.getInstance();
int year = minc.get(Calendar.YEAR);
minc.set(Calendar.YEAR,year-100);
datePickerDialog.getDatePicker().setMinDate(minc.getTimeInMillis());
datePickerDialog.show();
I have a date picker dialog, using which user can select a date. Conditions are :
Date must not be greater than today (system date)
Date must not be older than 3 months
I have done the first bit but I am not sure how to compare two dates to check if it is older than 3 months.
in the code : checkInYear, checkInMonth and checkInDay is what user selected and year, month, day is the system date.
Could you please suggest how to compare two dates to check if it is greater than 3 months.
Your help is much appreciated.
#Override
public void onDateChanged(DatePicker view, int selectedYear,
int selectedMonthOfYear, int selectedDayOfMonth) {
setCurrentDateOnView();
int checkInYear = selectedYear;
int checkInMonth = selectedMonthOfYear;
int checkInDay = selectedDayOfMonth;
if (checkInYear > year || checkInYear < year) {
view.updateDate(year, month, day);
}
if (checkInMonth > month && checkInYear == year) {
view.updateDate(year, month, day);
}
if (checkInDay > day && checkInYear == year
&& checkInMonth == month) {
view.updateDate(year, month, day);
}
}
};
Thank you very much
Just use according to your need.
String sDate = "05-10-2012"; // suppose you create this type of date as string then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(sDate);
Calendar c = Calendar.getInstance();
c.getTime().compareTo(date);
it depending on your string or how you can get? you can get all individually from datepicker then directly set in calendar instance
Calendar my = Calendar.getInstance();
my.set(year, month, day);
now compare
my.compareTo(Calendar.getInstance());
and for less then 3 months you can use something like ..
Date referenceDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(referenceDate);
c.add(Calendar.MONTH, -3);
return c.getTime();
On one of my app I'm using datepicker class to retrieve data form archive.
I need to set **Max and Min **. On DatePicker.Dialog,
i was able to set **Max to the current time ** however, i counted do for **Min **. if someone to help me i really appreciate it. thanks in advance!
I am looking set setMaxDate = to current year
and setMinDate = two years back from current year(current year -2)
NB: I triad to look if this question was already answered. But, i couldn't get one. all that i see is minData = current timedialog.getDatePicker().setMinDate(new Date().getTime()); which I am not looking for that. i want to set it to different year than the current.
here below part of my code:
// On Clike for Floting button with + signe
#Override
public void onClick(View v) {
switch (v.getId()) {
case imageButtonCalendar:
Calendar c = Calendar.getInstance();
final int[] mYear = {c.get(Calendar.YEAR)};
final int[] mMonth = {c.get(Calendar.MONTH)};
final int[] mDay = {c.get(Calendar.DAY_OF_MONTH)};
DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear[0] = year;
mMonth[0] = monthOfYear;
mDay[0] = dayOfMonth;
et.setText(new StringBuilder()
// to set date in editext
.append(mDay[0]).append("/").append(mMonth[0] + 1)
.append("/").append(mYear[0]).append(" "));
}
};
DatePickerDialog dialog = new DatePickerDialog(getActivity(), pDateSetListener, mYear[0], mMonth[0], mDay[0]);
dialog.getDatePicker().setMaxDate(new Date().getTime());
dialog.getDatePicker().setMinDate(new Date().getTime(),-year);
dialog.show();
break;
You can use Calendar class :
Calendar cal = Calendar.getInstance()
cal.add(Calendar.DATE, -7);
System.out.println("Date = "+ cal.getTime());
Joda-time is the best Java library for Date.
Get the date after subtracting 2 years :
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -2);
dialog.getDatePicker().setMinDate(calendar.getTime()); // set min date
I want to use a calendar in android. Within the calendar, I want to set the minimum year of 1998; T the user should not able to enter a year before that. How can I solve this problem? By default it goes before 1998.
I am using following code:
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(WinningNumberPowerBall.this, mDateSetListener, cyear, cmonth,cday);
mDatePickerDialog.show();
By using this my calendar year starts from 1900 but I want to start the year at 1998. How can I do this?
I think you can just do:
...
Calendar minCal = new GregorianCalendar(1998, Calendar.JANUARY, 1);
mDatePickerDialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
mDatePickerDialog.show();