setSelecteableDays Material DateTime Picker android - android

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

Related

How do I hardcode a specific default time [ANDROID]?

I am setting some default hardcoded times. I am looking to hardcode 6pm as my default time. I have the code as follows:
Calendar mDate = Calendar.getInstance();
SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy");
mydatetime = mDate.getTime();
For mydatetime I was looking to hardcode 5pm as the date time, but not sure how to go about it? any ideas?
You can set time to Calendar object as below:
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Use Calendar.HOUR_OF_DAY for 24 hour format.
Or: Calendar.HOUR for 12 hour format with calendar.set(Calendar.AM_PM, Calendar.PM); for setting AM or PM.
In your case you can use as below:
Calendar mDate = Calendar.getInstance();
mDate.set(Calendar.HOUR, 5);
mDate.set(Calendar.MINUTE, 0);
mDate.set(Calendar.AM_PM, Calendar.PM);
SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy, hh:mm a");
mydatetime = mDate.getTime();

Android DatePicker- GregorianCalendar - 00:00:00 UTC - getTimeInMillis()

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

Datepicker gives time not between exception

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());

Create date object from Date picker and Time Picker

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();

Current date should add 12 hours more in android

In my application i want to add 12 hours with the current date and time,then i have to show it in this format "yyyy-MM-dd HH:mm:ss". I wrote the code but unable to add 12 hours. How can i do? please help.
My code is :
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
The Calendar class has the add method which you can use to add certain units.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12);
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
How about
Calendar cal = Calendar.getInstance();
Date date = cal.getTime()+12*60*60*1000;
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
You should add the 12 hours to the Calendar object like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12)
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);

Categories

Resources