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.
Related
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 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.
I have 3 strings containing day, month and year values. For example:
String mday = "02";
String mmonth="07";
String myear="2013";
I need to set the DatePicker in my activity to a month from the date above. I do not mean just add 1 to the mmonth value... in case of day 31 I would end up with an invalid date.
So I need some way to increment the date (the valid way) and set the DatePicker with it's value.
I am aware that setting the datePicker with Int values is done like this:
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);
datepicker.init(iYear, iMonth, iDay, null);
// where iYear,iMonth and iDay are integers
But how do I obtain the integer values of day,month and year of an incremented DATE by one month?
So between the first values (strings) and final values of incremented date(integers) what are the steps that I must make?
I assume I would have to use a Calendar.
So my code should look like this:
Integer iYear, iMonth, iDay = 0;
String mday = "02";
String mmonth="07";
String myear="2013";
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(myear), Integer.parseInt(mmonth), Integer.parseInt(mday));
cal.add(Calendar.MONTH, 1);
// here I should get the values from cal inside the iYear, iMonth, iDay, but I do not seem to succeed.
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);
datepicker.init(iYear, iMonth, iDay, null);
if I do:
datepicker.init(cal.YEAR, cal.MONTH, cal.DATE, null);
then application crashes.
What should I do?
How to set this incremented by a month date into my DatePicker?
UPDATE
I changed my test code to this:
Calendar cal = Calendar.getInstance();
cal.set(2013, 05, 23);
cal.add(Calendar.MONTH, 1);
int xxday = cal.get(Calendar.DATE);
int xxmonth = cal.get(Calendar.MONTH);
int xxyear = cal.get(Calendar.YEAR);
datepicker.init(xxyear, xxmonth, xxday, null);
but Now the datePicker is set to one month from NOW instead of one month from the wanted date So instead of (2013-06-23) I have (2013-09-23). I assume it's because of
int xxmonth = cal.get(Calendar.MONTH);
how can I get the real month from a Calendar cal; ?
DatePicker class has a method updateDate(year, month, dayOfMonth) which you can use to set a date in your DatePicker as shown below:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.updateDate(2016, 5, 22);
Calendar month is 0 based. So month 07 is August.
Use the following code to initialize the calendar object if you have a date picker:
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
Else hard-code the date parts in the constructor
use this tuto to create your DatePickerDialog then use this code inside DatePickerDialog
https://developer.android.com/guide/topics/ui/dialogs.html
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mday = "02";
String mmonth="07";
String myear="2013";
//convert them to int
int mDay=Integer.valueOf(mday);
int mMonth=Integer.valueOf(mmonth);
int mYear=Integer.valueOf(myear);
return new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
String d=convertToCompletDate(i2,i1,i);
mListener.onDatePicked(d);
}
},mYear,mMonth,mDay);
}
In Kotlin
Assuming your date is a string. i.e:
var defaultDate = "20/4/2022"
you could use
val datePicker = findViewById<DatePicker>(R.id.date_Picker)
var defaultDate = eventDate.toString().split(Regex("/"))
var dd = defaultDate[0].toInt()
var mm = defaultDate[1].toInt()
var yy = defaultDate[2].toInt()
datePicker.updateDate(yy,mm,dd)
I can get the current date with the following code to show in DatePicker dialog for example.
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
How can I show the actual date minus 18years (more or less 6570days)?
From my "research" I need to use DAY_OF_YEAR
Try using:
c.add(Calendar.YEAR, -18);
You shouldn't have to worry about the "more or less" of 6570 days since a Calendar object handles leap years (and exact number of days in a month itself).
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();