I want to get next 5 years after from now and i am using this below code . It work fine in above android version 8 device .
@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("SimpleDateFormat")
fun getCalculatedYear(date: String, month: Int): String? {
val ld = LocalDate.parse(date)
val currentMonth = ld.minusYears(month.toLong())
Timber.tag("currentMonth").d(currentMonth.toString())
var dateFormat = SimpleDateFormat("yyyy-MM-dd")
val newDate = dateFormat.parse(currentMonth.toString())
dateFormat = SimpleDateFormat("yyyy")
return dateFormat.format(newDate!!)
}
Is there any solution to get this in all android version device.
Edited
Found solution with below code
@SuppressLint("SimpleDateFormat")
fun getCalculatedYears( year: Int): String? {
val c: Calendar = GregorianCalendar()
c.add(Calendar.YEAR, -year)
val sdfr = SimpleDateFormat("yyyy")
return sdfr.format(c.time).toString()
}
this helps me to find previous and next years