Issue with Gregorian Calendar and setting Day of the week

Hi

This may have a simple solution, but its currently got me stumped. Basically, after some calculation I determine the date that a particular event should occur on. These events are listed in a JTable where the column headers are the dates of the beginning of the weeks. In order to place my calculated event in the correct column I create a Gregorian Calendar, set it to the date of the event and then set the day of the week to the beginning of the week. Normally this works fine but for a few instances, rather than getting the date at the beginning of the week, I get the date thats the beginning of the next week.

As a quick example, to kind of illustrate my problem:

I determine that the date of the event is 27/06/2006 which is a Tuesday.

I want this event to appear in the column headed 25/06/2006, the date of the beginning of the week.

In fact it appears in the column headed the 02/07/2006 which is the beginning date of the following week.

Is there anyway to get it so when I set the day to the first day of the week it goes to the beginning of the current week rather than the beginning of the next week?

Any suggestions would be gratefully recieved as I'm currently struggling to think of any.

Thanks

[1272 byte] By [Ruanaea] at [2007-11-26 12:34:20]
# 1
Without suplying any code, I cannot conclude that you have logic errors. Perhaps doing that would help a bit.But jsut as a thought, why do you say Monday is the beginning of the week? I thought Sunday was.
Minkoa at 2007-7-7 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi, I dont say Monday is the begining of the week. At least I dont think I do.

As for code I'll post the conversion bit which is:

for (int i = 0; i < areaNameSelectionTable.getRowCount(); i++) {

GregorianCalendar conversionCalendar = new GregorianCalendar();

conversionCalendar.setTime((java.util.Date) prioritiesTable.getValueAt(i, 10));

conversionCalendar.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.SUNDAY);

...

}//end for loop

I then create a string from the GregorianCalendar in the format DD/MM/YYYY and compare that with the headers of my table columns (Also dates in the format DD/MM/YYYY) When they match thats the column this particular event should appear in. I can post this code if necessary, but I dont know if its relevant.

Thanks

Ruanaea at 2007-7-7 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 3
What is prioritiesTable?And why do you reset the day to be Sunday after each iteration?
Minkoa at 2007-7-7 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 4

I am working with a set of areas and Priorities table is a table where I calculate the date and priority of an event for each area I'm working with.

I then iterate through all areas (the for loop) and for each area in turn I get the date that the event should occur on. I set this date to Sunday so that it (in theory) becomes the start date of the week the event should occur in . I do this for every itereation because each area will have a different date associated with it.

Hope that makes things clearer

Ruanaea at 2007-7-7 15:49:44 > top of Java-index,Java Essentials,Java Programming...
# 5

Ok setting the date's day to Sunday doesnt help solve your problem. In fact it only screws it up (to my perspective)

I am going to assume your JTable contains column headers for every Sunday in the year (ie: every beginning of the week)

1)Then what I would do is get the date in the String format

2) Locate the month in the JTable where this date belongs

3) loop through all the column headers within that month and compare the day to that of that header in reverse order

4) if the day of the month is less than that of the day of the header then continue another iteration. If the day of the month is NOT less than the day of the header, you found your position in the JTable.

Again, this is from my perspective of information you gave me..if I am misinterpreting you let me know.

Minkoa at 2007-7-7 15:49:45 > top of Java-index,Java Essentials,Java Programming...
# 6
That sounds like a better way of doing it. I'll give it a try and let you knowThanks :-)
Ruanaea at 2007-7-7 15:49:45 > top of Java-index,Java Essentials,Java Programming...
# 7
HiI implemented your suggestion, and after fixing one daft mistake on my part if works fineThanks for your help :-)
Ruanaea at 2007-7-7 15:49:45 > top of Java-index,Java Essentials,Java Programming...
# 8
Glad to help. Thats what we are here for :]
Minkoa at 2007-7-7 15:49:45 > top of Java-index,Java Essentials,Java Programming...