1

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
  • 21
  • 3
  • 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

2 Answers2

1

LocalDate would be a lot easier.

You can create a LocalDate based on the date picker, and then use a built in method to add one month

import java.time.LocalDate

val today = LocalDate.now()

data class DatePicker(val day: Int, val month: Int)
val datePicker = DatePicker(15, 2)

val baseMonth = LocalDate.of(today.year, datePicker.month, datePicker.day)

// add one month
val nextMonth = LocalDate.from(baseMonth).plusMonths(1)

Note: The data class in the example is for demonstration purposes only. You could replace it with your actual date picker implementation.

Jade Steffen
  • 130
  • 2
  • 10
0

I've written function that gets a current day of month (integer) and return new day of month incremented by needed value. In my case it's a week. If you have a question please ask me.

@SuppressLint("SimpleDateFormat")
private fun getNewDayOfMonth(day: Int): String {
    val calendar = Calendar.getInstance()
    var dayOfMonth = day.toString()
    val simpleDateFormat = SimpleDateFormat("dd")
    calendar.time = simpleDateFormat.parse(dayOfMonth) as Date
    calendar.add(Calendar.DATE, 7)
    dayOfMonth = simpleDateFormat.format(calendar.time)
    return dayOfMonth
}
Raman
  • 1
  • 1