java8LocalDate Input the year and month on the command line, print out the calendar

java8 time class LocalDate 

LocalDate.now() Get the current time in the format: yyyy-MM-dd

LocalDate.of() "creates" the time class

date.getDayOfWeek() Get the day of the week and return the enumeration class (WEDNESDAY..)

date.getDayOfWeek().getValue() converted to int type

with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth(); Get the last day of the month

minusDays(1); minus one day

plusDays(1); add one day

getDayOfMonth() Get the day of the month

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份");
		int year = sc.nextInt();
		System.out.println("请输入月份");
		int month = sc.nextInt();
		LocalDate date = LocalDate.of(year, month, 1);
		System.out.println(date);
		int week = date.getDayOfWeek().getValue();
		int lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
		LocalDate nextDay = date.minusDays(1);
		System.out.println("一\t二\t三\t四\t五\t六\t日");
		for (int i = 1; i <= lastDay+week-1; i++) {
			if(i<week) {
				System.out.print("\t");
			}else {
				nextDay = nextDay.plusDays(1);
				System.out.print(nextDay.getDayOfMonth()+"\t");
			}
			if(i%7==0) {
				System.out.println();
			}
		}
		sc.close();
	}

Guess you like

Origin blog.csdn.net/weixin_53031149/article/details/127992359