I am using date picker in my application couples of time. I want my date picker to show dates following some restrictions. I am using the below code:-
#Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_PICKER_DIALOG:
datePickerDialogDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
year = selectedYear;
month = selectedMonth;
date = selectedDate;
activitySignUpDateEditText.setText(new StringBuilder().append(date));
activitySignUpMonthEditText.setText(new StringBuilder().append(month + 1));
activitySignUpYearEditText.setText(new StringBuilder().append(year));
}
};
return new DatePickerDialog(this, datePickerDialogDateSetListener, year, month, date);
default:
return null;
}
}
Somewhere i want the date picker to show maximum date 18 years back from current date & somewhere minimum date as the current date. I have searched a lot but doesn't found any satisfactory answer. I know about setMaxDate() & setMinDate() methods of date picker but when i tried with it, i am unable to set selected date on my edit text. any help would be appreciable. Thanks in advance.
You can try replacing this line:
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
By those:
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;
in the same way you set the minimum date as well
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;
For setting Max date as 18 years back, the following code snippet will do the job:
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 dateDialog = new DatePickerDialog(this, datePickerListener, year - 18, month, day);
//For setting minimum date as Current Date
//dateDialog.getDatePicker().setMinDate(c.getTimeInMillis ());
//For setting maximum date as 18 years back
c.set(year - 18, month, day);
dateDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return dateDialog;
As MaxDate can't be less than MinDate thus, MinDate should not be set as current date when MaxDate is set as 18 years back.
Hope it'll work for you.
Related
I have a DatePickerDialog in my app and I want the date selected by the user to be restricted until the current date. I am comparing the date selected from the DatePickerDialog with the newDate() and if the selected date is before, I am throwing an error. Please find the code as follows.
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String visitDate = dateFormat.format(calendar.getTime());
try {
Date appointDate = dateFormat.parse(visitDate);
Date currentDate = new Date();
if(appointDate.before(currentDate)){
appointmentDateInputLayout.setError("Date selected is not within range!");
}else{
appointmentDateEditText.setText(visitDate);
appointmentDateInputLayout.setError(null);
}
} catch (ParseException e) {
e.printStackTrace();
}
appointmentDateEditText.setText(visitDate);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
But when I do this, I am getting an error for selecting today's date also. I want the user to allow the current date. What am I missing here?
Kindly help.
Look, the
Date currentDate = new Date() ;
allocates the date object at the time it was instantiated in the degree of Milliseconds so alternating the allocation and putting date object at the first of the code would give the very early time so the time that you take to choose visitdate would be eventually greater (after) so the number of Milliseconds in the currentDate will be less than number of Milliseconds in the Picked new Date
Try to compare with calendar getTime() method instead of current date.:
If(appointDate.before(calendar.getTime())
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();
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
how to block only past date in DatePicker in android?
Am using sample code in that past date and current date getting blocked ,
I need only past date get blocked not current date here is my code
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int yearSelected,
int monthOfYear, int dayOfMonth) {
year = yearSelected;
month = monthOfYear;
day = dayOfMonth;
int n_mon = monthOfYear+1;
business_date_et.setText(day+"-"+n_mon+"-"+year);
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
business_time_et.setText(hour+":"+minute);
}
};
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
mYear, mMonth, mDay);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
da.getDatePicker().setMinDate(newDate.getTime());
return da;
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
Here is a sample code:
DatePicker datePicker = (DatePicker) getView().findViewById(R.id.your_datepicker_id);
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, c.getMinimum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getMinimum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getMinimum(Calendar.SECOND));
c.set(Calendar.MILLISECOND, c.getMinimum(Calendar.MILLISECOND));
Time now = new Time();
now.setToNow();
datePicker.updateDate(now.year, now.month, now.monthDay);
datePicker.setMinDate(c.getTimeInMillis());
Please note above that you must set MinDate value to the minimum time of the current day and not just the time which is 1 or 2 seconds behind the current time. This takes care of a well know problem of Datepicker in Android.
You can also try this:
da.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000)));
It basically subtracts the current date by the current offset of the day.
Use setMinDate method (minimum API 11):
datePicker.setMinDate(System.currentTimeMillis() - 1000);
For more information regarding your topic :
How to disable dates before today date in DatePickerDialog Android?
How to disable past dates in Android date picker?
You can get date of 1 day after current day with this:
c.add(Calendar.DATE, 1);
So by using
c.add(Calendar.DATE, 1);
You are setting date of tomorrow as Minimum date. So just change that to
c.add(Calendar.DATE, 0);
And it should set today's date as minimum date in datepicker.
Hope it helps.
I need to be able to choose date and time in my application
It should either be done by using a dialog, or starting a new intent.
The way I have done it now, is that I have started a new intent, since I need to set both date and time.
My problem is that I need to set the current date on the Datepicker (This is possible with the Timepicker using setCurrentHour and setCurrentMinute) onCreate, and set that as a lower boundary.
I can't find any functions in the Android API for doing this with the DatePicker.
Anyone have a suggestion to solve this problem?
Thanks in advance!
Edit:
Important: The DatePicker and TimePicker must be in the same dialog/intent
Straight to code
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();
Try this:
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
datepicker.init(year, month, day, null);
If you are simply looking for the equivalent of setCurrentHour() and setCurrentMinute() on TimePicker, but on DatePicker - see updateDate (int year, int month, int dayOfMonth) on DatePicker.
DatePicker mDatePicker = (DatePicker) findViewById(R.id.datePicker);
mDatePicker.updateDate(1994, 6, 12);
// Sets date displayed on DatePicker to 12th June 1994
You can query the current date through the Calendar class.
Calendar now = Calendar.getInstance();
now.get(Calendar.WHATDOYOUNEED);
Feed it's get() method with different parameters to customize your query.
The tutorial for the DatePicker seems to do exactly this?
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
Refer to:
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDisplay();
Check this it has pics to demonstrate.