Thufir Hawat Thufir Hawat - 11 months ago 72
Java Question

How to use Calendar.getInstance with specified Locale

I am trying to use

Calendar.getInstance(Locale l)
with specified
and is not working. I cannot figure out what I am doing wrong.

The Java Doc. say:

public static Calendar getInstance(Locale aLocale)

Gets a calendar using the default time zone and specified locale. The Calendar returned is based on the current time in the default time zone with the given locale.
aLocale - the locale for the week data
a Calendar.

My code:

public static void main (String[] args){

Locale local = new Locale("pt", "BR");

Calendar c = Calendar.getInstance(local); // here I am using the method
System.out.println(c.getTime()); // and here, I cannot figure out why is not working

DateFormat dt = DateFormat.getDateInstance(DateFormat.LONG, local);
String s = dt.format(c.getTime());
System.out.println(s); // here just a example in portuguese Brasil


Wed Apr 29 10:18:16 BRT 2015

29 de Abril de 2015

Should the first
must be in
Locale("pt", "BR")
, in portuguese?

Loc Loc
Answer Source

Your first print is just print Date information & Java using Default locale for this print (English)


This print will perform: c.getTime().toString() method, and this method using Default locale (ENGLISH) - You can take a look at Date.toString() source code to see Java using Default locale.

That is why the output is 'Wed Apr 29 10:18:16 BRT 2015'

If you want print output is in portuguese. You must go with Second print.