Question

I have a question for you that will be easy. I have a DatePickerDialog to select the day, month and year. Now I would like to receive the default next year, not the current one. I tried to insert + 1 but it is not the desired result. thanks

public void chooseDate2(View v) {
    new DatePickerDialog(Inserisci_en_us.this, d2,
                          dateAndTime.get(Calendar.YEAR),
                         dateAndTime.get(Calendar.MONTH),
                          dateAndTime.get(Calendar.DAY_OF_MONTH))
      .show();
  }
  private void updateLabel2() {
      scadenza.setText(fmtDateAndTime.format(dateAndTime.getTime()));           
  }
  DatePickerDialog.OnDateSetListener d2=new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        dateAndTime.set(Calendar.YEAR, year);
      dateAndTime.set(Calendar.MONTH, monthOfYear);
      dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
      updateLabel2();
    }
  };
Was it helpful?

Solution

I guess you want the next year as a default year when you open your date picker, right? I know you mentioned adding 1, but I guess you did it wrong. Just do the following:

Calenar c = Calendar.getInstance();
new DatePickerDialog(Inserisci_entrate_uscite.this, d2,
                      c.get(Calendar.YEAR) + 1,
                     dateAndTime.get(Calendar.MONTH),
                      dateAndTime.get(Calendar.DAY_OF_MONTH))
  .show();

This should return the next year even if many dialogs are opened. (If the dateAndTime is the current date).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top