I'm using a datepicker and want to set the min date to today and the max date to today one year ahead.
I do this like:
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
cal.add(Calendar.YEAR, 1);
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
When I don't do - 1000 then I get another exception:
java.lang.IllegalArgumentException: fromDate: Sat Apr 11 23:59:59 CEST 2015 does not precede toDate: Sat Apr 11 08:24:19 CEST 2015
thats because the date may not be equal to today. So I extract 1000 ms.
I don't know how to solve the new exception. I tried to count + 1000 ms on the maxDate but that didn't solve it.
EDIT:
I create my cal like this:
cal = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(getActivity(), this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
I solved the problem as follows:
cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
cal.add(Calendar.YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
I just set the start date to the minimum and the end date to the maximum off that day.
Looks like MinDate is higher than MaxDate. As per the exception.
Try this:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
cal.add(Calendar.YEAR, 1);
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
Thanks for sharing your solutions. I got a similar problem in one of my android apps. Also I want to limit the datepicker calendar range to show a minimum date 100 years ago and a maximum date of 5 years ago. Based on the codes that you shared, the following is my solution:
Calendar calmin = Calendar.getInstance();
Calendar calmax = Calendar.getInstance();
calmin.add(Calendar.YEAR, -100);
calmin.set(Calendar.HOUR_OF_DAY, calmin .getMinimum(Calendar.HOUR_OF_DAY));
calmin.set(Calendar.MINUTE, calmin .getMinimum(Calendar.MINUTE));
calmin.set(Calendar.SECOND, calmin .getMinimum(Calendar.SECOND));
calmin.set(Calendar.MILLISECOND, calmin.getMinimum(Calendar.MILLISECOND));
calmax.add(Calendar.YEAR, -5);
calmax.set(Calendar.HOUR_OF_DAY, calmax.getMaximum(Calendar.HOUR_OF_DAY));
calmax.set(Calendar.MINUTE, calmax.getMaximum(Calendar.MINUTE));
calmax.set(Calendar.SECOND, calmax.getMaximum(Calendar.SECOND));
calmax.set(Calendar.MILLISECOND, calmax.getMaximum(Calendar.MILLISECOND));
DatePickerDialog datePickerDialog = null;
datePickerDialog = new DatePickerDialog(ageGateActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth, dateSetListener, year,
month, day);
datePickerDialog.getDatePicker().setMinDate(calmin.getTimeInMillis());
datePickerDialog.getDatePicker().setMaxDate(calmax.getTimeInMillis());
Related
I'm trying to use the Material DateTimePicker library.
The problem is that when I set a minDate and maxDate in the datePicker and an array of selectable dates the dataPicker does not show what I'm aspected
Calendar minDate = Calendar.getInstance();
minDate.set(Calendar.YEAR, 2016);
minDate.set(Calendar.MONTH, 5);
minDate.set(Calendar.DAY_OF_MONTH, 1);
Calendar maxDate = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 10);
maxDate.set(Calendar.DAY_OF_MONTH, 30);
Calendar aDate = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 6);
maxDate.set(Calendar.DAY_OF_MONTH, 26);
Calendar aDate1 = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 5);
maxDate.set(Calendar.DAY_OF_MONTH, 22);
Calendar[] availableDate = new Calendar[2];
availableDate[0] = aDate;
availableDate[1] = aDate1;
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(ProvaDatePicker.this, availableDate[0].get(Calendar.YEAR), availableDate[0].get(Calendar.MONTH), availableDate[0].get(Calendar.DAY_OF_MONTH));
datePickerDialog.setMinDate(minDate);
datePickerDialog.setMaxDate(maxDate);
datePickerDialog.setSelectableDays(availableDate);
datePickerDialog.setTitle("Available date");
datePickerDialog.show(getFragmentManager(), "title");
This is the result
You are setting the dates in your code with YEAR to 2016, then in your screenshot you show that the YEAR is 2017.
To see the two enabled days you need to go to May 2016 or change the YEAR of your Calendar instances
You have the wrong assignement:
Calendar aDate = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 6);
maxDate.set(Calendar.DAY_OF_MONTH, 26);
Calendar aDate1 = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 5);
maxDate.set(Calendar.DAY_OF_MONTH, 22);
Here you should use aDate and aDate1 assignment, while you are assigning them to maxDate again and again
I want to show a specific month to user using android CalendarView. Please help me to implement this functionality. I wrote following code but it is not working.
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(2016, 6, 1);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2016, 6, 30);
calendarView.setMinDate(calendar1.DATE);
calendarView.setMaxDate(calendar2.DATE);
Following output is coming when i am trying to run the app using above code.
Hint: What is the the value of Calendar.DATE? Documentation says 5, and so when you call this method, you are saying "5 milliseconds past Jan 01, 1970."
setMinDate(long minDate)
Sets the minimal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Basically, that is a static field, and probably not the value that you want.
Perhaps you want getTimeInMillis() which returns the long for the value that you set the date at?
calendarView.setMinDate(calendar1.getTimeInMillis());
calendarView.setMaxDate(calendar2.getTimeInMillis());
As From my experience i always used Calender.set(Calendar.YEAR, year) method to set year and Calender.set(Calendar.MONTH, month) method to set month and Calender.set(Calendar.DAY,day) method to set day . So i am suggesting you to do same ,
String date = "1/6/2016";
String parts[] = date.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long milliTime = calendar.getTimeInMillis();
And now set the selected date in the calendar view by doing
mCalendarView.setDate (milliTime, true, true);
How do I set the Time of the GregorianCalendar to 00:00:00 UTC?
Because the following returns a Date at 10:00pm:
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(timeZone);
cal = new GregorianCalendar(year, monthOfYear, dayOfMonth ,0 ,0, 0);
editor.putString("auswahldatum", String.valueOf(cal.getTimeInMillis() / 1000))
.apply();
Log.i("Kalender", String.valueOf(cal.getTimeInMillis()));
Look at Calendar's Calendar.getInstance(timeZone) method it already return of new object of gregorian calendar. You can create it for example like this one:
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(timezone);
Or
Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth ,0 ,0, 0);
cal.setTime(date);
cal.setTimeZone(timezone);
Actually you must set time zone after time setting
I'm trying to create a date object in Android from Date picker and Time picker. I know how to do seperatly but when I want to create single Date object using both Date picker and Time picker
I tried this
DatePicker dp = (DatePicker)findViewById(R.id.datePickerDelayed);
TimePicker tp = (TimePicker)findViewById(R.id.timePickerDel);
Date myDbDate = new Date(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(), tp.getCurrentHour(), tp.getCurrentMinute());
but no luck as it is deprecated. Any one can please point me to a resource?
try to use Calendar, i'm working with it
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DATE, dayOfMonth);
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();
I have next code:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, mYear); /2011
cal.add(Calendar.MONTH, mMonth); /04
cal.add(Calendar.DAY_OF_MONTH, mDay);/13
cal.add(Calendar.HOUR, mHour); /11
cal.add(Calendar.MINUTE, mMinute); /53
System.out.println("Cal time "+ cal.getTimeInMillis());
System.out.println("System time " +System.currentTimeMillis());
cal time 64775494376227
System time 1302724616231
What's wrong in my code?
The problem is Calendar.getInstance() is already initialized to the current date/time. You're returning the millis time for for sometime in 4022...not 2011. See the JavaDoc for more info.