Solved

Creating a calendar...

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

713 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