Search code examples
javadatecalendarlocale

What is purpose of locale setting in Java java.util.Calendar?


I am obtaining information (such as number of a day in week, month, year etc) about particular date in Java via java.util.Calendar. Is there some reason to set locale for calendar object in my situation? I am asking beacuse:

System.out.println(cal.get(Calendar.DAY_OF_WEEK));

returns for today (Sunday) always number 1 but in our locale (cs_CZ) it should be 7.

Locale locale = new Locale("cs", "CZ");
TimeZone tz = TimeZone.getTimeZone("Europe/Prague");

Calendar cal = GregorianCalendar.getInstance(tz, locale);
cal.setTime(new Date());

// returns => 1 (but I expected 7)
System.out.println(cal.get(Calendar.DAY_OF_WEEK)); 
// returns => 3 - it's OK
System.out.println(cal.get(Calendar.DAY_OF_MONTH));

EDIT: I can hadle with 1 for Sunday, but I must be sure this is unchanging behaviour regardless to used Locale or TimeZone.


Solution

  • Finally I used Joda Time library:

    DateTime dt = new DateTime(new Date());
    int dayOfWeek = dt.getDayOfWeek();
    

    and it returns 7 for today (Sunday).