Solved

Creating a calendar...

Posted on 1998-02-06
4
165 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 84
sumDigits  challenge 7 60
IT Company 5 69
What is JNDI datasource in spring 1 38
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now