Solved

Creating a calendar...

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
eclipse argument 14 60
Arrays.asList  VS  ArrayList 4 54
Java: anonymous class 4 17
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 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