If you want to add months to one Calendar
, use the method add
, informing the field you want to add (in case, how I want to add the month, use Calendar.MONTH
):
Calendar calendar = Calendar.getInstance();
int quantidadeMeses = 3;
Locale local = new Locale("pt", "BR");
DateFormat dateFormat = new SimpleDateFormat("MMM", local);
for (int i = 0; i < quantidadeMeses; i++) {
// imprime a data correspondente ao Calendar
System.out.println(dateFormat.format(calendar.getTime()).toUpperCase());
calendar.add(Calendar.MONTH, 1); // soma 1 mês
}
In the above case, the Calendar
begins with today’s date (September), and within the for
i print the date and then add 1 month. The result is:
SET
OUT
NOV
Also note that you don’t need to create the SimpleDateFormat
within the for
, just create it once before the loop and reuse it at each iteration.
Besides, I use the method getTime()
, that returns a java.util.Date
, so that this is passed to the method format
.
java.time
If using Java >= 8 and your code does not depend on the use of Date
and Calendar
, an alternative is to use the API java.time
, which is more modern and solves many of the old API problems.
In your case, you could use a java.time.LocalDate
to save the date, use the method plusMonths
to add months, and format the date with a java.time.format.DateTimeFormatter
:
LocalDate data = LocalDate.now();
int quantidadeMeses = 3;
Locale local = new Locale("pt", "BR");
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM", local);
for (int i = 0; i < quantidadeMeses; i++) {
System.out.println(fmt.format(data).toUpperCase());
data = data.plusMonths(1); // somar 1 mês
}
Note that when adding 1 month I had to assign the return to the variable data
. This is because the classes of java.time
are immutable, and methods such as plusMonths
always return another instance with the modified values. The output is the same as the code with Calendar
.
Like the java.time
possesses several different classes to represent dates and times, you can also choose to use other of them depending on what you need.
In this case, if you don’t need the exact date (day, month and year), you could use a java.time.YearMonth
, which is a class that keeps only the month and year. Its operation is very similar to LocalDate
, because she also owns the method plusMonths
:
YearMonth data = YearMonth.now();
int quantidadeMeses = 3;
Locale local = new Locale("pt", "BR");
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM", local);
for (int i = 0; i < quantidadeMeses; i++) {
System.out.println(fmt.format(data).toUpperCase());
data = data.plusMonths(1); // somar 1 mês
}
Another alternative is to get the current date and extract only the month, getting a java.time.Month
, which in turn possesses the method plus
to add months:
Month mes = LocalDate.now().getMonth();
int quantidadeMeses = 3;
Locale local = new Locale("pt", "BR");
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM", local);
for (int i = 0; i < quantidadeMeses; i++) {
System.out.println(fmt.format(mes).toUpperCase());
mes = mes.plus(1); // somar 1 mês
}
And in this case, it would also be possible to use the method getDisplayName
, instead of using a DateTimeFormatter
:
Month mes = LocalDate.now().getMonth();
int quantidadeMeses = 3;
Locale local = new Locale("pt", "BR");
for (int i = 0; i < quantidadeMeses; i++) {
System.out.println(mes.getDisplayName(TextStyle.SHORT, local).toUpperCase());
mes = mes.plus(1); // somar 1 mês
}
If you don’t already use Java 8, you can use Threeten Backport, which has the same classes already mentioned (LocalDate
, DateTimeFormatter
, etc) and it basically works the same way. The difference is that they are in the package org.threeten.bp
(instead of java.time
). The backport is compatible with JDK 6 and 7.
In case you are still attached to Java 5 and want to use something better than Date
and Calendar
, an alternative is the Joda-Time (although this is a project considered "terminated", since on your site there’s a warning indicating this and recommending the use of java.time
)
Joda-Time has classes with names similar to java.time
, although it is not 100% equal. Classes are in the package org.joda.time
, follows a similar example to the previous code:
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
...
LocalDate date = new LocalDate();
Locale local = new Locale("pt", "BR");
DateTimeFormatter fmt = DateTimeFormat.forPattern("MMM").withLocale(local);
int quantidadeMeses = 3;
for (int i = 0; i < quantidadeMeses; i++) {
System.out.println(fmt.print(date).toUpperCase());
date = date.plusMonths(1);
}
More about the java.time
can be seen in this question.
I only want 3 letters anyway, the problem is that it always shows the "TEN" when I wanted it to show "SET", "OUT" and "NOV" in the case, that would be this month and the two subsequent
– EltonTozatto
@Eltontozatto I got it, I updated the answer
– hkotsubo
Perfect, worked as I wanted. VLW even!!
– EltonTozatto
Today I implemented using java.time that you recommended and worked perfectly, which is more practical than the first logic you used. Vlws
– EltonTozatto