Solved

Creating a calendar...

Posted on 1998-02-06
4
172 Views
Last Modified: 2011-09-20
I am trying to create a calendar applet and I am having difficulty with setting the days of the calendar.  I want the user to be able to select month and year, but when they do, how do u tell how many days the month has, and what day the first day of the month falls on?  How can I do these calculations?
0
Comment
Question by:bjg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1232920
It is easiest to hardcode the number of days in months (though you do need to make allowance for leap years).

Which day the first day of the month falls on could be done as follows:

assume the month and year selection are in variables of the same name:

Date ms=new Date(year-1900,month-1,1);
int sd=ms.getDay();

Date is a slightly strange class in the util package. It takes years (assuming 1900 as 0) and month (from 0-11). getDay returns the day of the week (0-6 with Sunday being 0).

0
 

Author Comment

by:bjg
ID: 1232921
How do I determine leap years?
0
 

Author Comment

by:bjg
ID: 1232922
How do I tell what day the first of each month falls on?

0
 
LVL 16

Expert Comment

by:imladris
ID: 1232923
A leapyear occurs when year%4==0 (i.e. if the remainder after dividing the year by 4 is 0).

The day the first of any particular month falls on is obtained by creating an instance of the date object as specified above. For March 1998, for example, you would have year set to 1998 and month to 3. Now create a date object for the first of March 1998:

Date ms=new Date(year-1900,month-1,1);

Adjustments are needed due to the way the date object is defined as explained before.
Now you have an object that represents the 1st of March 1998 and you can get various kinds of information from it, like the UTC (Universal Time Code), a String representing that date etc. etc. For our purposes you wish to know what day of the week this date falls on. The method "public int getDay();" will return a value from 0 to 6 (with Sunday being 0) indicating what day of the week it is on this date (March 1st, 1998).


0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question