how to add to a date in android

how to add to a date in android

To add a certain number of days to a date in Android, you can use the Calendar class and the add method. Here's an example of how to do this:

Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
calendar.add(Calendar.DATE, numberOfDaysToAdd);
Date newDate = calendar.getTime();
Sou‮ww:ecr‬w.lautturi.com

This will add the specified number of days to the original date and store the resulting date in the newDate variable.

If you want to add a different time unit (such as months or years), you can use a different constant from the Calendar class in place of Calendar.DATE. For example, to add one month to a date, you can use Calendar.MONTH.

It's also important to note that the Calendar class is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on. You will need to use one of its subclasses, such as GregorianCalendar, to actually create an instance of Calendar.

Created Time:2017-11-01 12:05:11  Author:lautturi