Two Laptop Bag*
The moose likes Beginning Java and the fly likes create calendar java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "create calendar java" Watch "create calendar java" New topic
Author

create calendar java

will sharp
Greenhorn

Joined: Oct 15, 2008
Posts: 4
So I have an assignment for school in which I need to get the month (assume February always has 28 days) and day of the week the first day of the month starts on. I can't use the calendar class in java. I know how to get the variables from the user but I'm not sure how to display the calendar.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

I'm a little confused about the assignment details. I'm assuming this is a command line application, right? Can you show us an example of what the input and output should look like when this runs?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
will sharp
Greenhorn

Joined: Oct 15, 2008
Posts: 4
Um something like...


Give me a month: (user inputs month)

What day does the first day of this month start on? (user inputs day)



Edit by mw: Added Code Tags to preserve formatting.
[ October 16, 2008: Message edited by: marc weber ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
something along tghese lines should do it (no guarantees though!)

you just need a single array 49 elements (for a 7 x 7 grid)
or you could use a 2D array
load the first 7 (0 to 6) with the days of the week Sun, Mon etc
important to make them "Sun ", "Mon " - if no space they'll run together SunMon
load the other 42 with 4 spaces each (alignment)

get the day of the week (number) for the first of the month
e.g. if it's a Thursday, it'll be 4 (0 is Sundays, if Sun is first in your calendar)
starting at element 11 (7 + 4, first 7 are for Sun Mon etc) make it
space space 1 space
then each element after that add 1
space space 2 space etc, until 10, then its
space 1 0 space, until last date in month is reach (28 for feb etc)

after you've loaded the array, print it out, and every 7 add a println

to redo, repeat the 4 spaces for elements 7 to 48
will sharp
Greenhorn

Joined: Oct 15, 2008
Posts: 4
I havent worked with arrays before, could you give me an example?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
simple demo - you should be able to work out the rest

will sharp
Greenhorn

Joined: Oct 15, 2008
Posts: 4




For the first line I have 7 numbers, and the rest of the lines its 8 numbers. Why is this? and how do I fix the spacing of the single numbers?

Thanks for helping me by the way.

[edit]Add code tags. CR[/edit]
[ October 16, 2008: Message edited by: Campbell Ritchie ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> For the first line I have 7 numbers, and the rest of the lines its 8 numbers.

arrays are 0-based, not 1-based as in your 'for loop'
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Please use the code button and preserve your indentation. I have edited your post so you can see how much better it looks.
 
 
subject: create calendar java
 
Similar Threads
Calendar with Java Swing
Is it possible to generate a random date with gregorian calendar?
Can i get month in words( eg:January) using Date,Calendar or GregorianCalendar API
Get first sunday of a month
Substracting days in java