I'm trying to understand how the Calendar, GregorianCalendar and Locale classes work.

Say I instantiate two Locale objects and then two Calendar objects, each with their respective Locale.

Then ask for the always troublesome DAY_OF_WEEK on every Calendar object as in the following code:

import java.util.Calendar;
import java.util.Calendar.*;
import java.util.*;

class Calendario {
	public static void main(String[] args) {
		Locale there = new Locale("ENGLISH", "US");
		Locale here = new Locale("GERMAN", "DE");
		GregorianCalendar gCalThere = new GregorianCalendar(there);
		GregorianCalendar gCalHere = new GregorianCalendar(here);		
		System.out.println("Day of week today there: " + gCalThere.get(GregorianCalendar.DAY_OF_WEEK));
		System.out.println("Day of week today here: " + gCalHere.get(GregorianCalendar.DAY_OF_WEEK));

The output is the following:

Day of week there: 4
Day of week here: 4

Being today wednesday, I'd have thougth "Day of week here" should be "3".

Why isnt'? How can I properly change Locale and display the correct day of the week?
Thanks in advance.

In many (most?) locales, Sunday is the first day of the week, thus Wednesday is the fourth. From the Calendar.java source:

     * Value of the {@link #DAY_OF_WEEK} field indicating
     * Wednesday.
    public final static int WEDNESDAY = 4;

You can set the first day of week to be whatever you want on your calendar object with setFirstDayOfWeek(int) (whether you should is another question altogether).

Hi, thanks Ezzaral.
Yeah, you are right, fiddeling with setFirstDayOfWeek(int) sounds messy.
I was trying to find a way to change the Locale and hoping that all the rest would change automatically (that was the attempt of the code I showed, anyway).
Any ideas on how to do that?

This article has been dead for over six months. Start a new discussion instead.