import java.util.Date; import java.util.Calendar; class YearExcption extends NegativeArraySizeException{ YearExcption(){ System.out.println("你输入的年数错误,请输入0~2100之间"); } } class MonthException extends NegativeArraySizeException{ MonthException(){ System.out.println("你输入的月份错误,请输入1~12之间"); } } class GregorianCalendar{ int year; int month; int date; int da[] = new int[31]; Calendar objcal; GregorianCalendar(int ye, int mo) throws YearExcption,MonthException { year = ye; month = mo - 1; objcal = Calendar.getInstance(); objcal.set(Calendar.YEAR, year); objcal.set(Calendar.MONTH, month); objcal.set(Calendar.DATE,1); if(ye<0||ye>2100){ throw new YearExcption(); } if(mo<1||mo>12){ throw new MonthException(); } } GregorianCalendar(){ objcal=Calendar.getInstance(); objcal.set(Calendar.DATE,1); this.nowshow(); } void nowshow(){ int year=objcal.get(Calendar.YEAR); int month=objcal.get(Calendar.MONTH); int day1 = objcal.get(Calendar.DAY_OF_WEEK);
int ct=0; this.comput(); System.out.println("\t\t\t"+year+"年"+(month+1)+"月"); System.out.println("\t日\t一\t二\t三\t四\t五\t六"); for (int i = 0; i < day1; i++) { System.out.print("\t"); } for (int i = 0; i < date; i++) { ct++; System.out.print(da[i] + "\t"); if (day1 + i == 7) { System.out.println(""); System.out.print("\t"); ct = 0; } else if (ct == 7) { System.out.println(""); System.out.print("\t"); ct = 0; } } } void comput() { if (month == 0 | month == 2 | month == 4 | month == 6 | month == 7 | month == 9 | month == 11) { date = 31; for (int i = 1; i <= 31; i++) { da[i - 1] = i; } } else if (month == 1) { if ((year % 4 == 0 & year % 100 != 0) | year % 400 == 0) { date = 29; for (int i = 1; i <= date; i++) { da[i - 1] = i; } } else { date = 28; for (int i = 1; i <= date; i++) { da[i - 1] = i; } } } else { date = 30; for (int i = 1; i <= date; i++) { da[i - 1] = i; } } } void show() { int ct = 0; this.comput(); int day2 = objcal.get(Calendar.DAY_OF_WEEK); System.out.println("\t\t\t" + year + "年" + (month + 1) + "月"); System.out.println("\t日\t一\t二\t三\t四\t五\t六"); for (int i = 0; i < day2; i++) { System.out.print("\t"); } for (int i = 0; i < date; i++) { ct++; System.out.print(da[i] + "\t"); if (day2 + i == 7) { System.out.println(""); System.out.print("\t"); ct = 0; } else if (ct == 7) { System.out.println(""); System.out.print("\t"); ct = 0; } } } }public class MonthTest { public MonthTest() { } public static void main(String[] args) { try{ int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); GregorianCalendar objgc = new GregorianCalendar(a, b); objgc.show(); }catch(ArrayIndexOutOfBoundsException ae){ GregorianCalendar objgca =new GregorianCalendar(); } } } 以前上学时写的一个。希望对你有帮助。这个可以看任何日期
import java.util.Calendar;
class YearExcption extends NegativeArraySizeException{
YearExcption(){
System.out.println("你输入的年数错误,请输入0~2100之间");
}
}
class MonthException extends NegativeArraySizeException{
MonthException(){
System.out.println("你输入的月份错误,请输入1~12之间");
}
}
class GregorianCalendar{
int year;
int month;
int date;
int da[] = new int[31];
Calendar objcal;
GregorianCalendar(int ye, int mo) throws YearExcption,MonthException {
year = ye;
month = mo - 1;
objcal = Calendar.getInstance();
objcal.set(Calendar.YEAR, year);
objcal.set(Calendar.MONTH, month);
objcal.set(Calendar.DATE,1);
if(ye<0||ye>2100){
throw new YearExcption();
}
if(mo<1||mo>12){
throw new MonthException();
}
} GregorianCalendar(){
objcal=Calendar.getInstance();
objcal.set(Calendar.DATE,1);
this.nowshow();
}
void nowshow(){
int year=objcal.get(Calendar.YEAR);
int month=objcal.get(Calendar.MONTH);
int day1 = objcal.get(Calendar.DAY_OF_WEEK);
int ct=0;
this.comput();
System.out.println("\t\t\t"+year+"年"+(month+1)+"月");
System.out.println("\t日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i < day1; i++) {
System.out.print("\t");
}
for (int i = 0; i < date; i++) {
ct++;
System.out.print(da[i] + "\t");
if (day1 + i == 7) {
System.out.println("");
System.out.print("\t");
ct = 0;
} else if (ct == 7) {
System.out.println("");
System.out.print("\t");
ct = 0;
} }
}
void comput() { if (month == 0 | month == 2 | month == 4 | month == 6 | month == 7 |
month == 9 | month == 11) {
date = 31;
for (int i = 1; i <= 31; i++) {
da[i - 1] = i;
}
} else if (month == 1) {
if ((year % 4 == 0 & year % 100 != 0) | year % 400 == 0) {
date = 29;
for (int i = 1; i <= date; i++) {
da[i - 1] = i;
}
} else {
date = 28;
for (int i = 1; i <= date; i++) {
da[i - 1] = i;
}
}
} else {
date = 30; for (int i = 1; i <= date; i++) {
da[i - 1] = i;
}
}
} void show() {
int ct = 0;
this.comput();
int day2 = objcal.get(Calendar.DAY_OF_WEEK);
System.out.println("\t\t\t" + year + "年" + (month + 1) + "月");
System.out.println("\t日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i < day2; i++) {
System.out.print("\t");
}
for (int i = 0; i < date; i++) {
ct++;
System.out.print(da[i] + "\t");
if (day2 + i == 7) {
System.out.println("");
System.out.print("\t");
ct = 0;
} else if (ct == 7) {
System.out.println("");
System.out.print("\t");
ct = 0;
} }
}
}public class MonthTest {
public MonthTest() {
} public static void main(String[] args) {
try{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
GregorianCalendar objgc = new GregorianCalendar(a, b);
objgc.show();
}catch(ArrayIndexOutOfBoundsException ae){
GregorianCalendar objgca =new GregorianCalendar();
} }
}
以前上学时写的一个。希望对你有帮助。这个可以看任何日期
}
http://topic.csdn.net/u/20080702/19/39d0d886-a7f0-4def-bf29-ea57e8911f0e.html