How to get current month with getDisplayName

Asked

Viewed 627 times

0

I intend to get the current month and for that 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 me April is giving me back March.

Can someone help me figure out what I’m putting wrong in getDisplayName?

  • Have you even tested the SimpleDateFormat with the Locale? I think this question might help you: http://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string.

  • @Wakim, I only use Locale to put the month in English.

  • 2

    The date of the system is correct?

  • @ramaral, yes that’s correct

1 answer

1

Running the above example, I did not get any error, returned the correct month. Before executing the code, check that the date of the system 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 looked like this:

Mes: April
Mes: April

Browser other questions tagged

You are not signed in. Login or sign up in order to post.