0

I am trying to create a gag calendar app and need some help getting the algorithm correct to create a calendar like this:

Sunday
Jan Feb .. Dec
  2   6      4
  9  13     11
 16  20     18
 23  27     25
 30

Monday
Jan Feb .. Dec
  3   7      5
 10  14     12
 17  21     19
 24  28     26
 31

And so on... I have the code from http://helpdesk.objects.com.au/java/display-a-month-as-a-calendar

But can't get the algorithm to do it like above.

MB34
  • 4,210
  • 12
  • 59
  • 110
  • what does your algorithm do at the moment? In what way is it not working. We can't help if you don't give us some clues. – Sam Holder Apr 05 '11 at 15:03
  • Have a look at this to see how to get the first weekday of a month, then for each new row just add 7 days to the previous row. http://stackoverflow.com/questions/924246/get-the-first-or-last-friday-in-a-month/924276#924276 – Mark Ransom Apr 05 '11 at 15:39
  • @Sam Holder, the code just prints a standard calendar of months. What I'm looking for is different as you can see. – MB34 Apr 05 '11 at 15:42
  • @Mark Ransom, not quite as simple as that. – MB34 Apr 05 '11 at 15:43
  • 1
    @MB34, why not? First row, display the first Sunday in Jan, first Sunday in Feb, etc. Second row show the second Sunday in Jan, second Sunday in Feb, etc. The method I linked will get the first, second, etc. specific day of any given month. – Mark Ransom Apr 05 '11 at 15:46
  • @MB34 that's too complicated? not sure how we can help if a fairly straightforward answer is too complicated. – Sam Holder Apr 05 '11 at 15:53
  • 1
    Another option is to build a set of nested objects, we used ArrayLists. It's a little grungy but the basic structure is ArrayList>> and represents a nested structure of month>; i.e. January, Tuesday<2,8,15,22,29>... Populate the required timespan up front and then display. – karakuricoder Apr 05 '11 at 16:31

1 Answers1

1

Model the desired output as a three-dimensional array

private static final int DAYS_IN_WEEK = 7;
private static final int WEEKS_IN_MONTH = 5;
private static final int MONTHS_IN_YEAR = 12;
Integer dayNumber[DAYS_IN_WEEK][WEEKS_IN_MONTH][MONTHS_IN_YEAR];

Allocate it and fill it using standard Calendar methods that give you the indices, then iterate over it in row-major order to produce the output.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190