打印日历
题目描述:
请按要求打印日历
输入:
只有一行, 为两个用空格分隔的整数, 第一个代表年份 (大于等于 1900 且小于等于 3000), 第二个代表月份 (保证在 1 到 12 之间).
输出:
该月的月历, 具体格式见样例.
注意: 输出中每一列都是 4 个字符, 如果不足 4 个则前边用空格填补. 第一列也是.
输入样例:
输出样例:
1 2 3 4 5 6
| Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
import java.util.*;
public class Main { public static void main(String[] args) {
var input = new Scanner(System.in); int year = input.nextInt(), month = input.nextInt(); month--; var calendar = new GregorianCalendar(year, month, 1);
int firstDayOfWeek = GregorianCalendar.MONDAY; calendar.setFirstDayOfWeek(Calendar.MONDAY);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
int indent = (weekday >= firstDayOfWeek) ? (weekday - firstDayOfWeek) : 6;
String[] weekdayNames = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; for (String weekdayName : weekdayNames) System.out.printf("%4s", weekdayName); System.out.println();
for (int i = 1; i <= indent; i++) System.out.print(" " );
do { int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.printf("%4d", day);
calendar.add(Calendar.DAY_OF_MONTH, 1); weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday == firstDayOfWeek) System.out.println(); } while (calendar.get(Calendar.MONTH) == month);
if (weekday != firstDayOfWeek) System.out.println(); } }
|
2023-04-03
IP属地: 北京