Aggiungere giorni ad un data in Kotlin
Oggi vediamo come aggiungere giorni ad una data usando Kotlin.
Usando DateFormat e LocalDateTime è abbastanza facile.
Ecco un esempio di codice:
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.*
fun main(args: Array<String>) {
val DATE_FORMAT = "yyyy-MM-dd"
val dateFormat: DateFormat = SimpleDateFormat(DATE_FORMAT, Locale.ITALIAN);
val date = Date();
val oggi = dateFormat.format(date);
println(oggi);
val localDateTime: LocalDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
val currentDatePlusOneDay: Date = Date.from(localDateTime.plusDays(10).atZone(ZoneId.systemDefault()).toInstant());
println(dateFormat.format(currentDatePlusOneDay));
}
Al metodo plusDays passate i giorni che volete aggiungere.
Se vedete la documentazione potete notare che avete anche i metodi per mesi, anni, ecc.
Enjoy!
kotlin dateformat localdatetime plusdays
Commentami!