import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
/**
* @author dj
* @version 1.0
**/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//输入你想要的月份的日历,1代表1月
int month_you_want = in.nextInt();
//可获得有关星期几的数组
String[] NameOfWeekdays = new DateFormatSymbols().getShortWeekdays();
GregorianCalendar now = new GregorianCalendar();
now.setTime(new Date());
//设置为你想要的月份
now.set(Calendar.MONTH,month_you_want-1);
int month = now.get(Calendar.MONTH);
for (int i = 1;i<=7;i++){
System.out.print(NameOfWeekdays[i]+"\t");
}
System.out.println();
// find the first day of this month
//找到该月的第一天是星期几
now.set(Calendar.DAY_OF_MONTH,1);
int theFirstDay = now.get(Calendar.DAY_OF_WEEK);
for (int i = 0;i<theFirstDay-1;i++){
System.out.print("\t\t");
}
// 第一行日期容易输出,先输出第一行日期
for(int i = 1;i<7 - theFirstDay+2;i++){
System.out.print(i+"\t\t");
}
System.out.println();
//将日期设置为日历第二行的首个日期
now.set(Calendar.DAY_OF_MONTH,7 - theFirstDay+2);
//便于接下来,每七个换一行
int numberOfLine = 0;
do{
System.out.print(now.get(Calendar.DAY_OF_MONTH)+"\t\t");
numberOfLine++;
if(numberOfLine ==7){
System.out.println();
numberOfLine = 0;
}
now.add(Calendar.DAY_OF_MONTH,1);
}while (now.get(Calendar.MONTH) == month);
//一旦进入下一个月,则程序就终止执行
}
}
java制作日历
最新推荐文章于 2024-06-09 20:04:58 发布