To add days to a date in Android using the Java programming language, you can use the Calendar or LocalDate class from the java.time library (starting from Android API level 26).
In Kotlin, you can use a similar approach to Java using the Calendar or LocalDate classes. Here is an example of its use:
Using the Calendar class:
import java.util.Calendar // ... // Start date val calendar = Calendar.getInstance() calendar.set ( 2024 , Calendar.JANUARY, 2 ) // Year, Month (starting from 0 for January), Day // Add 3 days val daysToAdd = 3 calendar.add(Calendar.DAY_OF_MONTH, daysToAdd) // Get the added date val year = calendar.get ( Calendar.YEAR ) val month = calendar.get ( Calendar.MONTH ) // Month starts from 0 val day = calendar.get (Calendar.DAY_OF_MONTH) // Use year, month, and day values as per your requirement.
Using class
LocalDate
:import java.time.LocalDate // ... // Start date val date = LocalDate.of( 2024 , 1 , 2 ) // Year, Month, Day // Add 3 days val daysToAdd = 3 val newDate = date.plusDays(daysToAdd.toLong()) // Get the added date val year = newDate.year val month = newDate.monthValue // Month starts from 1 val day = newDate.dayOfMonth // Use year, month, and day values as per your requirement.
Be sure to adjust the year, month, and day values to suit your needs. Kotlin has a more concise and expressive syntax, but the basic concepts remain the same as Java.