Pregunta

by default, the start of Activity I want to display the current date but with next year. I added one, but in the TextView at startup I see the current date. Thank you for your help.

public void chooseDate2(View v) {
    new DatePickerDialog(Activity.this, d1,
                          dateAndTime1.get(Calendar.YEAR) + 1,
                         dateAndTime1.get(Calendar.MONTH),
                          dateAndTime1.get(Calendar.DAY_OF_MONTH))
      .show();
  }
  private void updateLabel2() {
    tv.setText(fmtDateAndTime.format(dateAndTime1.getTime()));           
  }
  DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        dateAndTime1.set(Calendar.YEAR, year);
      dateAndTime1.set(Calendar.MONTH, monthOfYear);
      dateAndTime1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
      updateLabel2();
    }
  };
¿Fue útil?

Solución

use

return new DatePickerDialog(this, datePickerListener, year+1, month,
                    day);

and

 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            datehere.setText(new StringBuilder().append(year).append("-")
                    .append(month + 1).append("-").append(day).append(" "));

        }
    };
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top