Java例子:万年历的输出

/**
 * 
 */
package 练习0927;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Scanner;

public class 日期类的练习 {

	public static void main(String[] args) throws Exception {
		printCalendar();
	}

	private static void printCalendar() {
		int year;
		int month;
		int monthDay;
		Scanner scanner = new Scanner(System.in);

		System.out.println("请输入年:");
		year = scanner.nextInt();
		System.out.println("请输入月:");
		month = scanner.nextInt();

		LocalDate date = LocalDate.of(year, month, 1);
		DayOfWeek lo = date.getDayOfWeek();// 星期几

		LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
		monthDay = lastDayOfThisMonth.getDayOfMonth();// 多少天

		System.out.println("日\t一\t二\t三\t四\t五\t六");
		int spaceNum = 0;// 统计第一行应该打印的空格数
		switch (lo) {
		case SUNDAY:
			spaceNum = 0;
			break;
		case MONDAY:
			spaceNum = 1;
			break;
		case TUESDAY:
			spaceNum = 2;
			break;
		case WEDNESDAY:
			spaceNum = 3;
			break;
		case THURSDAY:
			spaceNum = 4;
			break;
		case FRIDAY:
			spaceNum = 5;
			break;
		case SATURDAY:
			spaceNum = 6;
			break;

		default:
			break;
		}

		for (int i = 0; i < spaceNum; i++) {
			System.out.print('\t');
		}
		// 打印
		int oneLineNumCount = spaceNum + 1;
		for (int firstLineNum = 1; firstLineNum <= monthDay; firstLineNum++) {
			if (oneLineNumCount == 7) {
				System.out.print(firstLineNum + "\n");
				oneLineNumCount = 1;
			} else {
				System.out.print(firstLineNum + "\t");
				oneLineNumCount++;
			}

		}
	}

}

 
 

你可能感兴趣的:(javaSE学习)