关闭

java 打印日历

标签: 日历java打印日历
2525人阅读 评论(0) 收藏 举报
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class 打印日历 {
	public static void main(String[] args) throws ParseException {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要显示的年份");
		int year = input.nextInt();
		System.out.println("请输入要显示的月份");
		int month = input.nextInt();
		int count = getdataCount(year, month);
		cal.setTime(sdf.parse(year + "-" + month + "-" + 1));
		int first = cal.get(Calendar.DAY_OF_WEEK);
		System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
		for (int i = 1; i < first; i++) {
			System.out.print("\t");
		}
		for (int i = 1; i <= count; i++) {
			System.out.print(i + "\t");
			cal.setTime(sdf.parse(year + "-" + month + "-" + i));
			if (cal.get(Calendar.DAY_OF_WEEK) == 7) {
				System.out.println();
			}
		}
	}
	private static int getdataCount(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		int count = cal.getActualMaximum(Calendar.DATE);
		return count;
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59983次
    • 积分:1226
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:1篇
    • 译文:0篇
    • 评论:8条