Test.java
package com.liu;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar_copy calendar = new Calendar_copy();
}
}
Calendar_copy.java
package com.liu;
import java.util.*;
public class Calendar_copy {
public Calendar_copy() {
// TODO Auto-generated constructor stub
int in = (int) (Math.random() * 1000) + 2000;
int mon = 29;
if (in % 4 == 0 && in % 100 != 0 && in % 400 == 0)
mon = 28;
System.out.println(in + "年2月的日历项为:");
System.out.println(" 日 一 二 三 四 五 六 ");
Calendar calendar = Calendar.getInstance();
calendar.set(in, 2, 1);
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
String a[] = new String[week + mon];
for (int i = 0; i < week; i++) {
a[i] = "**";
}
for (int i = week, n = 1; i < week + mon; i++) {
if (n <= 9) {
a[i] = String.valueOf(n) + " ";
} else {
a[i] = String.valueOf(n);
}
n++;
}
for (int i = 0; i < a.length; i++) {
if (i % 7 == 0) {
System.out.println("");
}
System.out.print(" " + a[i]);
}
}
}
运行结果:
2385年2月的日历项为:
日 一 二 三 四 五 六
** ** ** ** ** 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29