Setting minimum year in Calendar - android

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

Related

setMaxDate to DatePickerDialog

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

How can i get the String name of day on DatePicker

I'm developing a simple app that the user pick some date on DatePicker and return the current day name in some Toast message.
but when I'm trying to do that it has given me a wrong date.
my code:
final int day = datepicker.getDayOfMonth();
datepicker.getDrawableState();
int mounth = datepicker.getMonth();
mounth+=1;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, day);
String formatDay = new SimpleDateFormat("E").format(cal.getTime());
Toast.makeText(MainActivity.this, formatDay, 2000).show();
You're setting the day of month when you create the Calendar, but you aren't setting the month or year. You need to getMonth() and getYear() of the DatePicker and set those on the Calendar also. Do GregorianCalendar cal = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0) instead of Calendar.getInstance(). With getInstance() you are getting a Calendar for the current time, then just changing the day.

Android set DatePicker to a certain date

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)

Android Date Picker - Actual Date minus X number of days

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).

How do I set the current date in a DatePicker?

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.

Categories

Resources