Modify Dates of each object - MONTH, YEAR

1

I have an app where I create a posting of for example 400 reais and before saving I set the creation date and how many times I have to do it. Then saved.

And right now, unless I add the post to a% launch%. If I did it twice, I have to get the date I set and modify the dates, for example:

30/10 first installment and after 30/11 to Monday.

How can I change this release date?

    
asked by anonymous 03.11.2016 / 00:41

1 answer

0

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

    
03.11.2016 / 00:44