package com.wq.main;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Scanner;
/**
* @version 创建时间:2018年8月1日 上午10:25:02 java8 Instant 万年历
*/
public class InstantDate {
private static Scanner sc = new Scanner(System.in);
private static String year = "";
private static String month = "";
private static String day = "";
static final Integer NUMBER_ONE = 1;
public static void main(String[] args) throws ParseException {
Integer years = year();
Integer months = month();
Integer days = day();
Calendar(years, months, days);
}
public static void Calendar(Integer year, Integer month, Integer day) {
// 根据年月算出每个月一号是星期几
Integer week = LocalDate.of(year, month, NUMBER_ONE).getDayOfWeek().getValue();
// 根据年月算出月的天数,能区分润年平年
Integer days = YearMonth.of(year, month).lengthOfMonth();
System.out.println("\t" + year + "年\t" + month + "月" + day + "日");
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int j = 0; j < (week == 7 ? 0 : week); j++) {
System.out.print("\t");
}
for (int j = 1; j <= days; j++) {
if (j == day) {
System.out.print(j + "*" + "\t");
} else {
System.out.print(j + "\t");
}
if ((week + j) % 7 == 0) {
System.out.print("\n");
}
}
}
// 判断年份输入是否合法
private static Integer year() {
System.out.println("请输入年:");
while (true) {
year = sc.nextLine();
if (year.length() == 0 || year == null) {
System.out.println("请输入年:1900-9999_null");
} else if (!year.matches("[0-9]+")) {
System.out.println("请输入年:1900-9999_不是数字");
} else if (Integer.parseInt(year) < 1900 || Integer.parseInt(year) > 9999) {
System.out.println("请输入年:1900-9999_不在范围");
} else {
return Integer.parseInt(year);
}
}
}
// 判断月份输入是否合法
private static Integer month() {
System.out.println("请输入月:");
while (true) {
month = sc.nextLine();
if (month.length() == 0 || month == null) {
System.out.println("请输入月:1-12_null");
} else if (!month.matches("[0-9]+")) {
System.out.println("请输入月:1-12_不是数字");
} else if (Integer.parseInt(month) < 1 || Integer.parseInt(month) > 12) {
System.out.println("请输入月:1-12_不在范围");
} else {
return Integer.parseInt(month);
}
}
}
// 判断月天数输入是否合法
private static Integer day() {
System.out.println("请输入日:");
// 根据年月算出月的天数,能区分润年平年
Integer days = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month)).lengthOfMonth();
while (true) {
day = sc.nextLine();
if (day.length() == 0 || day == null) {
System.out.println("请输入日:1-" + days + "_null");
} else if (!day.matches("[0-9]+")) {
System.out.println("请输入日:1-+" + days + "+_不是数字");
} else if (Integer.parseInt(day) < 1 || Integer.parseInt(day) > days) {
System.out.println("请输入日:1-" + days + "_不在范围");
} else {
return Integer.parseInt(day);
}
}
}
}
运行效果图: