Link to home
Start Free TrialLog in
Avatar of bjg
bjg

asked on

Creating a calendar...

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?
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bjg
bjg

ASKER

How do I determine leap years?
Avatar of bjg

ASKER

How do I tell what day the first of each month falls on?

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).