Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Calendar Issue

Ok, I have a problem where: given two dates and a frequency, a list of dates are returned in that range based on that frequency.

The dates are formatted mm/dd/yyyy where leading zeros are not included.
The frequency is formatted in one of 3 ways:
1) Daily - Occurs every day of week
2) X... - Occurs every day of week except the dates after the X
3) ... - Occurs only on the days indicated
The days in the frequency are determined by a number, ranging 1 to 7 where 1 is Monday and 7 is Sunday. So, the frequency X6 would say: "Every day except Saturday" and the frequency 126 would say: "Only on Monday, Tuesday, and Saturday".

Now, to accomplish this task, I have a method that returns a List<Date>. Inside that method, I have a boolean array of 7 where each index is a day of the week (where Monday is index 0). I then set the booleans true/false based on the frequencies. After that, I compute each day in the range and check its Day Of Week to see if it is in the frequency. If it is, I add it to my List<Date>, if not, I exclude it. Here is my code for that method:
```    public List<Date> getDates(String star,String end,String frequency)
{
//Where frequencies[0] == Monday
boolean[] frequencies = new boolean[7];

if(frequency.equalsIgnoreCase("Daily"))
{
for(int i=0;i<frequencies.length;i++)
{
frequencies[i]=true;
}
}
else if(frequency.contains("X") || frequency.contains("x"))
{
for(int i=0;i<frequencies.length;i++)
{
frequencies[i] = true;
}
String n = frequency.substring(1);
for(int i=0;i<n.length();i++)
{
int v = Integer.parseInt(n.substring(i,i+1));
frequencies[v-1] = false;
}
}
else
{
for(int i=0;i<frequency.length();i++)
{
int v = Integer.parseInt(frequency.substring(i,i+1));
frequencies[v-1] = true;
}
}

String[] eSplit = star.split("/");
String[] dSplit = end.split("/");
Date d1 = new Date(Integer.parseInt(eSplit[2]),Integer.parseInt(eSplit[0])-1,Integer.parseInt(eSplit[1]));
Date d2 = new Date(Integer.parseInt(dSplit[2]),Integer.parseInt(dSplit[0])-1,Integer.parseInt(dSplit[1]));
List<Date> dates = new ArrayList<Date>();
long interval = 1000 * 60 * 60 * 24;
long endTime = d2.getTime();
long curTime = d1.getTime();
while (curTime <= endTime)
{
Date temp = new Date(curTime);
Calendar check = new GregorianCalendar(temp.getYear(), temp.getMonth(), temp.getDate());
check.setFirstDayOfWeek(Calendar.MONDAY);
int dayOfWeek = check.get(Calendar.DAY_OF_WEEK);
if(frequencies[dayOfWeek-1])
curTime += interval;
}
return dates;
}```

The problem I am having is that the dates it returns are incorrect. Specifically, it would seem it is 1 day early and for some it doesn't even find dates. For example, for in restriction of:
1/4/2011 to 2/16/2011 for 6:
it returns:
1/7/2011
1/14/2011
1/21/2011
1/28/2011
2/4/2011
2/11/2011
while it should return:
1/8/2011
1/15/2011
1/22/2011
1/29/2011
2/5/2011
2/12/2011

Another example is that for the restriction of:
12/21/2010 to 1/3/2011 for 6:
it returns:
12/24/2010
12/31/2010
while it should return:
12/25/2010
1/1/2011

Any clues as to what is happening? I need to figure this out fairly quickly.

2. ## Re: Calendar Issue

I figured it out. Apparently Calendar.setFirstDayOfWeek(Calendar.MONDAY); doesnt exactly set the first day of week to be Monday. It is more complicated than that. Either way, I figured it out.