当月日历打印
import java.util.*;public class CalendarStudy {public static void main(String[] args) {printCurrentMonthCalendar();}private static void printCurrentMonthCalendar() {/* 1.獲取當前日期 月與日信息* 2.獲取當前月 第一天信息(如第一天為星期幾)* 3.循環開始打印,格式控制,結束條件日期不為當前月*/Calendar date = new GregorianCalendar();int nowMonth = date.get(Calendar.MONTH);int nowDay = date.get(Calendar.DAY_OF_MONTH);date.set(Calendar.DAY_OF_MONTH, 1);int firstWeekday = date.get(Calendar.DAY_OF_WEEK); // 當前月第一天為星期幾System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六");for (int i = Calendar.SUNDAY; i < firstWeekday; i++) {System.out.printf("%7s", " ");}while (date.get(Calendar.MONTH) == nowMonth) {int day = date.get(Calendar.DAY_OF_MONTH);int weekDay = date.get(Calendar.DAY_OF_WEEK);System.out.printf("%6d", day);if (day != nowDay) {System.out.print(" ");} else {System.out.print("*");}if (weekDay == Calendar.SATURDAY) {System.out.println();}date.add(Calendar.DAY_OF_MONTH, 1);}}
}
總結
- 上一篇: Climbing Stairs -- L
- 下一篇: 如何做好软件售前和售前的心得体会