Java method show dates in a loop

1

Good evening, I have a method where I get the amount of installments and the starting date. I want to add 1 month to this initial looping date and I'm confused, giving quite a different value:

public void faturar(int parcela, String dataFaturamento) {

    try {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dtFatura = df.parse(dataFaturamento);
        Calendar c = Calendar.getInstance();
        c.setTime(dtFatura);
        for (int i = 0; i < parcela; i++) {
            // Através do Calendar, trabalhamos a data informada e adicionamos 1 mês 
            c.add(Calendar.MONTH +1,i);
            Date dtParcelasGeradas = c.getTime();  
            System.out.println(dtParcelasGeradas );
        }

I wanted my System.out.println to look like this:

  

09/19/2016   10/19/2016   11/19/2016   12/19/2016   1/19/2017

But the way I'm doing it is showing like this:

Wed Sep 07 00:00:00 BRT 2016   Wed Sep 14 00:00:00 BRT 2016  Wed Sep 28 00:00:00 BRT 2016
    
asked by anonymous 20.09.2016 / 00:16

1 answer

5

To display in DD / MM / YYYY format you need to use DateFormat. The big problem in your code is only in the c.add line (Calendar.MONTH + 1, i), the right one is c.add (Calendar.MONTH, 1) because the function expects which attribute will be incremented and the second parameter is the amount that will be incremented.

I did it here, and it worked the way you needed it.

public static void faturar(int parcela, String dataFaturamento) {


        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dtFatura = null;
        try {
            dtFatura = df.parse(dataFaturamento);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(dtFatura);
        for (int i = 0; i < parcela; i++) {
            // Através do Calendar, trabalhamos a data informada e adicionamos 1 mês 
            c.add(Calendar.MONTH, 1);
            Date dtParcelasGeradas = c.getTime();  
            System.out.println(df.format(dtParcelasGeradas ));

        }
    }

    
20.09.2016 / 00:50