简单的demo
1 package com.test.transport.service; 2 3 import java.text.DateFormatSymbols; 4 import java.util.Calendar; 5 import java.util.Locale; 6 7 public class DisplayCalendar { 8 public static void main(String args[]) { 9 Locale.setDefault(Locale.US); 10 Calendar calendar = Calendar.getInstance(); 11 calendar.setLenient(true); 12 int today = calendar.get(Calendar.DAY_OF_MONTH);//当天 13 int month = calendar.get(Calendar.MONTH);//当月 14 int firstDayOfWeek = calendar.getFirstDayOfWeek();//每周从周几开始 15 calendar.set(Calendar.DAY_OF_MONTH, 1); 16 int firstday = calendar.get(Calendar.DAY_OF_WEEK); 17 String[] weekdayNames = new DateFormatSymbols().getShortWeekdays(); 18 for(int i=1;i<8;i++){ 19 System.out.printf("%4s", weekdayNames[i]); 20 } 21 System.out.println(); 22 do { 23 int day = calendar.get(Calendar.DAY_OF_MONTH); 24 if(day == 1){ 25 System.out.print(" "); 26 for(int i =0;i<firstday-1;i++){ 27 System.out.print(" "); 28 } 29 } 30 System.out.printf("%3d", day); 31 if (day == today) { 32 System.out.print("*"); 33 } else { 34 System.out.print(" "); 35 } 36 calendar.add(Calendar.DAY_OF_MONTH, 1); 37 if (calendar.get(Calendar.DAY_OF_WEEK) == firstDayOfWeek) { 38 System.out.println(); 39 System.out.print(" "); 40 } 41 } while (calendar.get(Calendar.MONTH) == month); 42 } 43 }
输出结果: