自己做出来的万年历:
以下代码:
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份和月份:");
int y = sc.nextInt();
int m = sc.nextInt();
GregorianCalendar gc = new GregorianCalendar();
int theYear = gc.get(Calendar.YEAR);
int theDay = gc.get(Calendar.DATE);
//把年份和月份进行设置
gc.set(Calendar.YEAR, y);
gc.set(Calendar.MONTH, m);
System.out.println(gc.get(Calendar.YEAR));
while(gc.get(Calendar.YEAR) == y) {
System.out.println("现在是:" + gc.get(Calendar.YEAR) + "年" + (gc.get(Calendar.MONTH) + 1) + "月");
int month = gc.get(Calendar.MONTH);
gc.set(Calendar.DATE, 1);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for(int i = 0; i < gc.get(Calendar.DAY_OF_WEEK) - 1; i++) {
System.out.print(" ");
}
while(gc.get(Calendar.MONTH) == month) {
int day = gc.get(Calendar.DATE);
if(day < 10) {
System.out.print(" ");
}
System.out.print(" " + day);
if(gc.get(Calendar.DATE) == theDay) {
if(gc.get(Calendar.MONTH) == Calendar.JUNE) {
if(gc.get(Calendar.YEAR) == theYear) {
System.out.print("*");
}
}
} else {
System.out.print(" ");
}
if(gc.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
System.out.println();
}
gc.add(Calendar.DATE, 1);
}
System.out.println();
return;
/*if(gc.get(Calendar.MONTH) == Calendar.JUNE) {
if(gc.get(Calendar.YEAR) == theYear) {
System.out.println("===================================");
}
} else {
System.out.println("----------------------------------");
}
*/
}
}
}