Create date object from Date picker and Time Picker - android

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

Related

setSelecteableDays Material DateTime Picker 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

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

How to set a custom minimum and maximum date selection day in "wdullaer materialdatetimepicker"

I am working on a project where I need to restrict the user from choosing any other dates than I want.
by reading some articles and SO post i tried the following which is supposed to work
com.wdullaer.materialdatetimepicker.date.DatePickerDialog dpd = newInstance(
ActivityClaimOffers.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setMinDate(calendar);
dpd.setMaxDate(calendar);
but I
could not figure out how to pass custom dates as calendar objects.
as both setMinDate() and setMaxDate() takes Calendar as parameter
Like this :
Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);
and for your code:
dpd.setMinDate(new GregorianCalendar(2000, 5, 23));
Hey try like this code I have done like this
Calendar nowMinimum = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
this,
nowMinimum.get(Calendar.YEAR),
nowMinimum.get(Calendar.MONTH),
nowMinimum.get(Calendar.DAY_OF_MONTH));
dpd.setMinDate(nowMinimum); //today's date is minimum
Calendar thenMaximum = Calendar.getInstance();
thenMaximum.set(year, monthOfYear, dayOfMonth);// you can pass your custom date
dpd.setMinDate(thenMaximum);
dpd.setTitle("Select Date");
dpd.show(getActivity().getFragmentManager(), "date_picker");

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

How to get date & time in 13 digit(epoch time) from day,month,year,hours and minutes?

I am working on an application in which I have got date and time from similar dialog.
I have all this variable of type integer.
Now I have to convert this all variables in to the epoch time.
It is very important for me.
Is there any way to do this?
You can use Calendar Class, you must instantiate a new Calendar Object, set MONTH,DAY,YEAR,HOUR,MINUTE and SECOND and then use getTime() method.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR, hours);
cal.set(Calendar.MINUTE, minute);
int epoch_time = (int) cal.getTimeInMillis()/1000;
Use Calendar:
Calendar c = Calendar.getInstance();
c.set(year, month-1, date, h, min, sec);
long t = c.getTimeInMillis();

Categories

Resources