package samples;
import java.util.*;
/**
* <p>Title: Sample2DMonths</p>
* <p>Description: Just a example 2D array </p>
* <p>Copyright: Perry Anderson</p>
* @author Perry Anderson
* @version 1.0
*/
public class Sample2DMonths {
int year;
Calendar [] months;
String days[] = { "Unknown", "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
private void calcMonthsoftheYear(int year) {
this.months = new GregorianCalendar[12];
for (int i=0; i<months.length; i++)
this.months[i] = new GregorianCalendar(year, i, 1);
}
public void printMonths() {
System.out.println("For the year "+year);
for (int i=0; i<months.length; i++) {
System.out.print("Days in month " + i + " = " +
months[i].getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println("\tFirst of the month starts on a " +
days[months[i].get(Calendar.DAY_OF_WEEK)]);
}
}
public Sample2DMonths(int year) {
calcMonthsoftheYear(year);
this.year = year;
}
public static void main(String[] args) {
Sample2DMonths year1999 = new Sample2DMonths(1999);
Sample2DMonths leapYear = new Sample2DMonths(2000);
year1999.printMonths();
leapYear.printMonths();
}
}
perry wrote:
package samples;
import java.util.*;
/**
* <p>Title: Sample2DMonths</p>
* <p>Description: Just a example 2D array </p>
* <p>Copyright: Perry Anderson</p>
* @author Perry Anderson
* @version 1.0
*/
public class Sample2DMonths {
int year;
int [] daysInMonth;
public int [] getMonthsoftheYear(int year) {
int [] results = new int[12];
for (int i=0; i<results.length; i++) {
Calendar month = new GregorianCalendar(year, i, 1);
results[i] = month.getActualMaximum(Calendar.DAY_OF_MONTH);
}
return results;
}
public void printMonths() {
System.out.println("For the year "+year);
for (int i=0; i<daysInMonth.length; i++)
System.out.println("Days in month "+i+" = "+daysInMonth[i]);
}
public Sample2DMonths(int year) {
daysInMonth = getMonthsoftheYear(year);
this.year = year;
}
public static void main(String[] args) {
Sample2DMonths year1999 = new Sample2DMonths(1999);
Sample2DMonths leapYear = new Sample2DMonths(2000);
year1999.printMonths();
leapYear.printMonths();
}
}
Stewart Gordon wrote:
ch******************@yahoo.co.in wrote:
hi,
I have a problem, I am not able to find out which day of the week it
is using the calendar class.
I am using set() function to set the date and want to find out which
day i.e mon,tue etc of the week it is .
get(Calendar.DAY_OF_WEEK)
http://java.sun.com/j2se/1.4.2/docs/api/
Stewart.