Solution 1 (Recommended): In Java 8 you can use the new JSR-310
API based on Joda-Time
. This library can also be downloaded to Java 7 here :
LocalDateTime.from(data.toInstant()).plusDays(1);
Solution 2: You can also use Joda-Time
, which facilitates enough use of date and time elements in Java:
DateTime dataJoda = new DateTime(data);
dataJoda = dataJoda.plusMonths(1);
Solution 3: Use class Calendar
as follows, where variable data
is Date
with date set:
Calendar c = Calendar.getInstance();
c.setTime(data);
c.add(Calendar.MONTH, 1);
data = c.getTime();
This solution is not recommended following the extensive explanation given in this topic #, which comes down to:
DO NOT USE ANY OF THESE CLASSES. They are full of defects - I would need a whole answer just to discuss it - and there are much better alternatives.
Calculation method
Using JSR-310
the implementation would look similar to the example below:
private List<LocalDateTime> calcularDatas(LocalDateTime dataBase, Integer quantidade) {
List<LocalDateTime> resultado = new ArrayList<>();
Integer indice;
for (indice = 0; indice < quantidade; indice++) {
resultado.add(dataBase.plusMonths(indice));
}
return resultado;
}
Reference: How to add one day to a date? , Difference between Date, sql .Date and Calendar