Java.util.Calendar - week_of_year problem

F

Frank Lenaerts

Hello,

I seem to have encountered a strange problem with the java.util.Calendar
object. When the week starts on a Monday, no problems, but when it starts
on a Saturday... The problem is: when I try to set the WEEK_OF_YEAR
property for 2004, 2005, 2006 (haven't tested it further), it always returns
a week before the week I want to set. With 2003 everything's ok. An
example of the used code:


### CODE ###

// Code used in the main thread ...
Calendar cal = Calendar.getInstance();
cal = Util.setWeekOfYear(cal, week, year);

// Somewhere else ...
public static Calendar setWeekOfYear(Calendar cal, int weekOfYear, int year)
{

cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.SATURDAY)
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);

System.out.println("[UTIL] date: " +
GlobalDateFormatter.format(cal.getTime()));
System.out.println("[UTIL] week: " + cal.get(Calendar.WEEK_OF_YEAR));

return cal;

}

### OUTPUT ###

The good situation
(week = 1; year = 2003):

[UTIL] date: 05/01/2003
[UTIL] week: 1

(week = 30; year = 2003):

[UTIL] date: 27/07/2003
[UTIL] week: 30

The bad situation
(week = 1; year = 2004):

[UTIL] date: 29/12/2003
[UTIL] week: 52

(week = 30; year = 2004):

[UTIL] date: 19/07/2004
[UTIL] week: 29


Anyone knows what the problem could be ? We're using JDK 1.3.0-C. Thank
you very much in advance!

Frank
 
D

Dario

Frank said:
Hello,

I seem to have encountered a strange problem with the java.util.Calendar
object. When the week starts on a Monday, no problems, but when it starts
on a Saturday... The problem is: when I try to set the WEEK_OF_YEAR
property for 2004, 2005, 2006 (haven't tested it further), it always returns
a week before the week I want to set.

No problem here with JDK 1.4.2.

Here the program:

import java.util.Calendar;
class Quiz61 {
public static Calendar cal = Calendar.getInstance();
public static Calendar setWeekOfYear(Calendar cal,
int weekOfYear, int year) {
System.out.println("week = " + weekOfYear + "; year = " + year);
cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.SATURDAY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
System.out.println("[UTIL] date: " + cal.getTime());
System.out.println("[UTIL] week: " + cal.get(Calendar.WEEK_OF_YEAR));
System.out.println();
return cal;
}
public static void main(String[] args) {
cal = setWeekOfYear(cal, 1, 2003);
cal = setWeekOfYear(cal, 30, 2003);
cal = setWeekOfYear(cal, 1, 2004);
cal = setWeekOfYear(cal, 30, 2004);
}
}

Here the result:

week = 1; year = 2003
[UTIL] date: Mon Jan 06 17:01:05 CET 2003
[UTIL] week: 1

week = 30; year = 2003
[UTIL] date: Mon Jul 28 17:01:05 CEST 2003
[UTIL] week: 30

week = 1; year = 2004
[UTIL] date: Mon Jan 05 17:01:05 CET 2004
[UTIL] week: 1

week = 30; year = 2004
[UTIL] date: Mon Jul 26 17:01:05 CEST 2004
[UTIL] week: 30
 
G

Gregory A. Swarthout

Frank Lenaerts said:
Hello,

I seem to have encountered a strange problem with the java.util.Calendar
object. When the week starts on a Monday, no problems, but when it starts
on a Saturday... The problem is: when I try to set the WEEK_OF_YEAR
property for 2004, 2005, 2006 (haven't tested it further), it always returns
a week before the week I want to set. With 2003 everything's ok. An
example of the used code:


### CODE ###

// Code used in the main thread ...
Calendar cal = Calendar.getInstance();
cal = Util.setWeekOfYear(cal, week, year);

// Somewhere else ...
public static Calendar setWeekOfYear(Calendar cal, int weekOfYear, int year)
{

cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.SATURDAY)
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);

System.out.println("[UTIL] date: " +
GlobalDateFormatter.format(cal.getTime()));
System.out.println("[UTIL] week: " + cal.get(Calendar.WEEK_OF_YEAR));

return cal;

}

### OUTPUT ###

The good situation
(week = 1; year = 2003):

[UTIL] date: 05/01/2003
[UTIL] week: 1

(week = 30; year = 2003):

[UTIL] date: 27/07/2003
[UTIL] week: 30

The bad situation
(week = 1; year = 2004):

[UTIL] date: 29/12/2003
[UTIL] week: 52

(week = 30; year = 2004):

[UTIL] date: 19/07/2004
[UTIL] week: 29


Anyone knows what the problem could be ? We're using JDK 1.3.0-C. Thank
you very much in advance!

Frank

Haven't a clue, other than to say that the following line looks like the culprit:

cal.setMinimalDaysInFirstWeek(4);

Greg
 
P

P.Hill

Gregory said:
Haven't a clue, other than to say that the following line looks like the culprit:

cal.setMinimalDaysInFirstWeek(4);

setMinimalDaysInFirstWeek sure is the problem.

Since you set this value to 4 that means that you have
defined the calendar to start counting weeks of the year
with the week that has AT LEAST 4 DAYS in the current year,
the partial week which starts 2004 which only has 3 days in
2004 is by the settings you gave it, part of the previous year.

Hope that helps,
-Paul
 
Joined
Apr 14, 2009
Messages
1
Reaction score
0
Encountered this kind of problem...
Solved it by forcing minimalDaysInFirstWeek to 1.

Hope that Java 7 will come with a nicer Date API.

Neoludo.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top