Java
需要确定文件名(main函数所在类名于文件名一致才能编译)
文件名: Test.java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Test {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int month = date.getMonthValue();
int today = date.getDayOfMonth();
date = date.minusDays(today - 1); // the first day of the month
DayOfWeek week = date.getDayOfWeek();
int value = week.getValue();
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 0; i < 4*value - 4;++i)
System.out.print(' ');
while (date.getMonthValue() == month) {
System.out.printf("%3d", date.getDayOfMonth());
if (date.getDayOfMonth() == today)
System.out.print('*');
else
System.out.print(' ');
date = date.plusDays(1);
if (date.getDayOfWeek().getValue() == 1) System.out.println();
}
if (date.getDayOfWeek().getValue() != 1) System.out.println();
}
}
Python
from datetime import *
today = date.today()
month = today.month
day = today.day
week = today.weekday() + 1
firstday = today.replace(day=1)
firstdayWeek = firstday.weekday() + 1
print("Mon Tue Wed Thu Fri Sat Sun")
for i in range(4 * firstdayWeek - 4):
print(' ', end='')
while firstday.month == month:
print("%3d" % firstday.day, end='')
if firstday.day == day:
print('*', end='')
else:
print(' ', end='')
firstday += timedelta(days=1)
if firstday.weekday() == 0:
print()
if firstday.day != 0:
print()
效果
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 31*