A friendly place for programming greenhorns!
Printing a Calendar in Java
For this problem, you will be creating two methods: printMonth and printYear.

For printMonth(int year, int month, int janFirst, boolean leapYear), the int year represents the year in question, month will be a number from 1-12 representing the month (January-December).  janFirst will be a number from 0-6 (Sunday-Saturday) representing the day of the week of January 1st, and leapYear will be true if the year is a leap year.

printMonth(2016, 2, 5, true) should print:

February 2016
Su  Mo  Tu  We  Th  Fr  Sa
1    2    3    4   5    6
7    8    9  10   11 12  13
14   15  16  17   18 19  20
21   22  23  24   25 26  27
28   29

printYear(int year, int janFirst, boolean leapYear) is similar.  However, it will indicate special holidays with certain characters:

President’s Day   *p Third Monday in Feb
Veteran’s Day     *v November 11
Thanksgiving     *t 4th Thursday in November
Memorial Day   *m Last Monday in May
Christmas           *c December 25
New Years           *y January 1st
4th of July           *4 July 4th
Labor Day           *L First Monday in September
Tax Day           *x April 15th (16th if Sunday, 17th if Saturday)

So, printYear(2013, 2, false) should print:

2013

January                         February                        March
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
*y   2   3  4   5                            1  2                              1  2
6   7   8   9 10 11 12    3    4  5    6   7  8  9    3     4  5    6  7   8  9
13 14  15 16 17 18 19  10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 *p 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31            24 25 26 27 28             24 25 26 27 28 29 30
31
April                                May                              June
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
1   2   3   4  5   6                    1  2  3  4                                  1
7   8   9 10 11 12 13    5    6  7   8  9 10 11      2   3   4   5   6  7   8
14 *x 16 17 18 19 20   12 13 14 15 16 17 18      9 10 11 12 13 14 15
21 22 23 24 25 26 27   19 20 21 22 23 24 25    16 17 18 19 20 21 22
28 29 30                      26*m 28 29 30 31        23 24 25 26 27 28 29
30
July                                August                     September
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
1   2   3 *4  5  6                         1  2   3     1  *L  3   4   5  6   7
7  8  9 10 11 12 13       4   5   6    7  8  9  10     8  9 10 11 12 13 14
14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
28 29 30 31                 25 26 27 28 29 30 31   29 30

October                         November                    December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
1  2   3   4  5                             1  2     1   2   3   4   5   6  7
6   7   8  9 10 11 12     3   4   5   6   7   8  9     8   9 10 11 12 13 14
13 14 15 16 17 18 19   10 *v 12 13 14 15 16    15 16 17 18 19 20 21
20 21 22 23 24 25 26   17 18 19 20 21 22 23    22 23 24 *c 26 27 28
27 28 29 30 31            24 25 26 27 *t  29 30    29 30 31

Victoria,
CodeRanch is Not a Code Mill While we are happy to answer specific questions, nobody is going to write your project for you. So what more specific question do you have?
This is what I have attempted doing:

I don't know exactly how to display all the dates starting from the beginning date of each month. Also, the method I am using to find the first day of each month doesn't seem to work. Is anyone able to help?
Why have you got all your class members static?
Have you been through the Java™ Tutorials, because I think you will find lots of useful information there?
Never write == true or == false. Both are poor style and error‑prone; you might write = instead of == by mistake.
Never
if (b == true) ...
always
if (b) ...
Never
if (b == false)...
always
if (!b)...
Note that expressions are easier to read if you can avoid the bang sign ! for not.

Campbell Ritchie wrote:Why have you got all your class members static?
Have you been through the Java™ Tutorials, because I think you will find lots of useful information there?

All my classes/methods are static because the main method needs to be able to access them. Without the static, the main doesn't recognize them.
That is bad design, I am afraid. It means you have too much code in the main() method.

Campbell Ritchie wrote:That is bad design, I am afraid. It means you have too much code in the main() method.

I don't see how that is. All the main method is doing is executing the two methods printMonth() and printYear().
One problem as per my understanding is your return statement is out side the loop, so it will print only when the loop terminates.  You either need some sort of recursive function or the print statement should be inside the loop itself.

Swastik Dey wrote:One problem as per my understanding is your return statement is out side the loop, so it will print only when the loop terminates.  You either need some sort of recursive function or the print statement should be inside the loop itself.

I changed the return statement and added a bunch of System.out.print() statements. This is what I have now:

Although the code works for February 2016, it does not seem to do so for most other dates. How am I able to revise this?

What is happening in the above line?

Swastik Dey wrote:

What is happening in the above line?

I was trying to find the first date of the month, and I think that this is the part of the code that needs work.
What is the reason behind starting from January?  Why not start from the 1st of the given month and year itself.

Swastik Dey wrote:What is the reason behind starting from January?  Why not start from the 1st of the given month and year itself.

According to my assignment, I'm required to have a user input the day that January 1st is on and figure out the other first days based on January 1st.
Ok..then even things are getting complicated.  You can use Calendar, class it would be much easier.

Swastik Dey wrote:Ok..then even things are getting complicated.  You can use Calendar, class it would be much easier.

Yeah, it would probably be easier to do that, but imports besides java.util.scanner aren't allowed (you can probably feel my pain). I just tried this:

In this piece of code, I tried to add the days of each month to 'd'. I then modded it to find the day that the month should start at. However, this still doesn't yield the right days, so I'm unsure how to fix this.
What value janFirst contain.   Is this fixed value.

Swastik Dey wrote:What value janFirst contain.   Is this fixed value.

No, it depends on what year it is. The user will have to input that value. For example, 2016's janFirst is 5, or Thursday.
Note that it is not necessary for the caller to specify what day January 1 is (not only that, it is just wrong to do so - the caller could easily enter the incorrect day).
Knowing that January 1, 2018 was a Monday, it is not too difficult to deduce what day January 1 is for any specified year.

It is also not necessary to include a parameter that specifies whether it is a leap year (that also allow the caller to make an error).

Fred Kleinschmidt wrote:Note that it is not necessary for the caller to specify what day January 1 is (not only that, it is just wrong to do so - the caller could easily enter the incorrect day).
Knowing that January 1, 2018 was a Monday, it is not too difficult to deduce what day January 1 is for any specified year.

It is also not necessary to include a parameter that specifies whether it is a leap year (that also allow the caller to make an error).

Sorry, it would be easier to have an automatic calendar that is not based on user input, but that is a requirement of the assignment.

Victoria Li wrote:. . . . that is a requirement of the assignment.

Many of us believe that one of the things one should never do is write one's own date and time classes. But you are stuck with it.
So your requirement is , considering the given value for Jan you have to display the calendar of given month?

Swastik Dey wrote:So your requirement is , considering the given value for Jan you have to display the calendar of given month?

Yep! I just can't get the first day for each month right.
(1 like)
A simple way without calculations is to have a 2D array month[][], where the first index is the month, and the second index the number of days in that month. So month[0] = new int[31], month[1] = new int[28/29], et cetera. Then loop through all the months and days, starting with day janFirst.

Piet Souris wrote:A simple way without calculations is to have a 2D array month[][], where the first index is the month, and the second index the number of days in that month. So month[0] = new int[31], month[1] = new int[28/29], et cetera. Then loop through all the months and days, starting with day janFirst.

Oh! I completely forgot about ragged arrays! I'll try that. Thanks
So I've got this code now:

It works for me, but now I need to print out the entire calendar:

printYear(int year, int janFirst, boolean leapYear) is similar.  However, it will indicate special holidays with certain characters:

So, printYear(2013, 2, false) should print:

The special characters (i.e. *p, *v, etc.) should be in red.

Any idea how to add in the special characters using the shortest amount of characters?
Also, calling printMonth() in printYear() will print out the months, but in a vertical line. Any idea how to make it print in a 4*3 formation (4 down, 3 across)
What about printing a month to a String[], that later can be printed on screen or paper? You just calculate how many lines maximum it would take to print a month (including headers) and how long the longest line is. Once you have that, you can print the months any way you like by combining the strings.

This thread has been viewed 469 times.

All times above are in ranch (not your local) time.
The current ranch time is
Nov 13, 2018 22:05:15.