monday as first day of the week

S

sunspot

hey,

I'm having trouble to tell JAVA that monday is the first day of the week and
not sunday. This is what I have at the moment

Calendar time;
time = Calendar.getInstance();
time.setFirstDayOfWeek(Calendar.MONDAY);

when I output time.get(Calendar.DAY_OF_WEEK) it always gives me a number on
higher then it actually is since it assumes Sunday is day 1. This really is
a annoying bug in my program. Can anyone help me or set me on the right
track ?

thnx in advance
 
T

Thomas Weidenfeller

sunspot said:
Calendar time;
time = Calendar.getInstance();
time.setFirstDayOfWeek(Calendar.MONDAY);

when I output time.get(Calendar.DAY_OF_WEEK) it always gives me a number on
higher then it actually is since it assumes Sunday is day 1. This really is
a annoying bug in my program. Can anyone help me or set me on the right
track ?

Your assumption how the API works is wrong, not the API's "assumption"
about the numeric value of the SUNDAY constant.
get(Calendar.DAY_OF_WEEK) does not return some offset number depending
on the specified first day of the week. It returns the fixed values of
the day constants as defined in the class. SUNDAY is defined as 1, so it
is absolutely correct that get(Calendar.DAY_OF_WEEK) returns 1 on Sundays.

Any code which relies on the numeric values returned by
get(Calendar.DAY_OF_WEEK) is broken.

/Thomas
 
G

Gordon Beaton

I'm having trouble to tell JAVA that monday is the first day of the
week and not sunday. This is what I have at the moment

Calendar time;
time = Calendar.getInstance();
time.setFirstDayOfWeek(Calendar.MONDAY);

when I output time.get(Calendar.DAY_OF_WEEK) it always gives me a
number on higher then it actually is since it assumes Sunday is day
1. This really is a annoying bug in my program. Can anyone help me
or set me on the right track ?

If today is Thursday then you should get Calendar.THURSDAY regardless
of which day the week started on. The value of this constant does not
change when you change the start of the week from Sunday to Monday,
since doing so does not renumber the weekdays.

setFirstDayOfWeek() is used together with setMinimalDaysInFirstWeek()
in order to number the weeks correctly.

/gordon
 
F

Filip Larsen

sunspot wrote
I'm having trouble to tell JAVA that monday is the first day of the week and
not sunday. This is what I have at the moment

Calendar time;
time = Calendar.getInstance();
time.setFirstDayOfWeek(Calendar.MONDAY);

when I output time.get(Calendar.DAY_OF_WEEK) it always gives me a number on
higher then it actually is since it assumes Sunday is day 1.

I think you misunderstand the API.

The call to time.get(Calendar.DAY_OF_WEEK) returns a constant
representing the day for the date in question, for instance
Calendar.MONDAY on mondays, and this has nothing to do with what day is
first in a week. First day in a week is only used during calculation of
WEEK_OF_MONTH and WEEK_OF_YEAR.

If you want to know how many days has passed since monday then you can
do something like

int daysSinceMonday =
(7 + time.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY) % 7;


Regards,
 
S

sunspot

thnx guys

int daysSinceMonday = (7 + time.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY)
% 7;

seems to have done the trick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top