0

I created a datepicker and users are able to pick a date from it, in another textview i want to show the date of one exact month later. (E.g. User chooses 25th of February, the view will show 25th of March)

val simpleDateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
        val getDate :Calendar = Calendar.getInstance()
        val datepicker = DatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth,DatePickerDialog.OnDateSetListener
        { datePicker, i, i2, i3 ->
            val selectDate :Calendar = Calendar.getInstance()
            selectDate.set(Calendar.YEAR,i)
            selectDate.set(Calendar.MONTH,i2)
            selectDate.set(Calendar.DAY_OF_MONTH,i3)
            val date :String = simpleDateFormat.format(selectDate.time)
            sulusText.setText(date)
            
        },getDate.get(Calendar.YEAR),getDate.get(Calendar.MONTH),getDate.get(Calendar.DAY_OF_MONTH))
        datepicker.show()
    }
}

So here user can choose the date with sulustext and in another view i'd like show the date of one month later.

MrKoala
  • 11
  • 2
  • I recommend you don’t use `SimpleDateFormat` and `Calendar`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 07 '21 at 05:08
  • Does this answer your question? [Adding months to dates \[closed\]](https://stackoverflow.com/questions/46151369/adding-months-to-dates). In particular look at [the answer by user7605325](https://stackoverflow.com/a/46156503/5772882). – Ole V.V. Dec 07 '21 at 05:13
  • Thanks for the answers. I figured out adding one more line of these codes "selectDate.set(Calendar.YEAR,i) selectDate.set(Calendar.MONTH,i2+1) selectDate.set(Calendar.DAY_OF_MONTH,i3) " solved my problem. I'll try to change my code as Ole V.V. said anyway. Thanks again! – MrKoala Dec 07 '21 at 19:36

0 Answers0