File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

create calendar java

 
will sharp
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
will sharp
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havent worked with arrays before, could you give me an example?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple demo - you should be able to work out the rest

 
will sharp
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button and preserve your indentation. I have edited your post so you can see how much better it looks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic