13

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; ?

user1137313
  • 2,390
  • 9
  • 44
  • 91
  • what is the stacktrace of the crash? I assume nullpointer – Kenny C Aug 03 '13 at 00:52
  • If I do this: `Calendar cal = Calendar.getInstance(); cal.set(2013, 07, 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);` then My datepicker is set to 23 september 2019. Instead of 23.08.2013. I assume it's because I access the Calendar.MONTH which is set for today ?!?!? – user1137313 Aug 03 '13 at 00:55
  • FATAL Exception: main (Illegal Argument exception - current should be >= start and <= end). This is for previous code. As I stated in my comment above, I found a way to set it to a different date, but not my custom date... so I receive no more errors, I just don't have the date I want – user1137313 Aug 03 '13 at 00:58
  • Print some debug info with System.out.println(cal.getTime()); e.g. 1) date after calendar.getInstance() , 2) date after setting your date, 3) date after adding your month 4) before adding your date to datepicker. it would help you find where the problem actually is. – Manolis Proimakis Aug 03 '13 at 01:45
  • Please read all comments. I solved the problem. I am just interested if the method I am using now is valid. I mean are there cases when my destination date could be invalid using my method? The method I am talking about is: `cal.add(Calendar.MONTH-1, 1);` – user1137313 Aug 03 '13 at 02:10

5 Answers5

28

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);
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Hradesh Kumar
  • 1,765
  • 15
  • 20
4

Calendar month is 0 based. So month 07 is August.

Kenny C
  • 2,279
  • 1
  • 19
  • 20
  • So I can basically do this: `cal.add(Calendar.MONTH-1, 1);` ? Is it safe? – user1137313 Aug 03 '13 at 01:18
  • I see that if I do the above code, it works... but if my initial date is Jan.31.2013, then my final date in the DatePicker is Mar.03.2013. So I guess adding a month to a calendar date is adding 31 days to it? Is it normal? – user1137313 Aug 03 '13 at 01:21
3

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

indivisible
  • 4,892
  • 4
  • 31
  • 50
2

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);
}
Amirouche Zeggagh
  • 3,428
  • 1
  • 25
  • 22
2

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)
sajeyks mwangi
  • 549
  • 8
  • 20