Displaced Hoser Displaced Hoser - 1 year ago 66
Java Question

DAY_OF_WEEK always returns 7 in Java GregorianCalendar

So I want to do something with Java GregorianCalendar that seems to be going way harder than it should. I want to get the day of the week from the month and date. But it won't do that. I know that people often get the wrong answer to this because they don't know month is numbered from 0 while DAY_OF_WEEK is numbered from 1 (like here and here). But that's not my problem. My problem is that DAY_OF_WEEK always, always, returns 7, no matter what I set the date to. When I convert the GregorianCalendar to string, DAY_OF_WEEK appears as ?, even though I have set year, month, and dayOfMonth.

The code where this is happening:

GregorianCalendar theDate;

public ObservableDate(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)
theDate = new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute, second);
System.out.println("DAY_OF_WEEK = "+theDate.DAY_OF_WEEK);

The code that calls it:

ObservableDate theDate = new ObservableDate(2001, 3, 24, 9, 00, 00);



Any idea why this is happening?

Answer Source

You are accessing the field constant instead of getting its' value. I believe you wanted to use something like,

System.out.println("DAY_OF_WEEK = " + theDate.get(Calendar.DAY_OF_WEEK));