How to get current month with getDisplayName

0

I want to get the current month and for this I am using the following code:

Calendar calendar = Calendar.getInstance();
String mes = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH);

The problem is that for example now instead of returning April is returning me March .

Can anyone help me figure out what I'm putting down on getDisplayName ?

    
asked by anonymous 04.04.2015 / 23:47

1 answer

1

Running the above example, I did not get any error, returned the correct month. Before running the code, make sure that the system date on which the JVM is running is correct.

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;


public class Testes {

public static void main(String args[]) {
    Calendar calendar = Calendar.getInstance();
    String mes = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH);
    System.out.println("Mes: " + mes);

    mes = GregorianCalendar.getInstance().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH);
    System.out.println("Mes: " + mes);
}

}

Output looks like this:

Mes: April
Mes: April
    
06.04.2015 / 17:36