直接上代码吧
1 import java.text.DateFormat; 2 import java.text.ParseException; 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 import java.util.GregorianCalendar; 7 8 /** 9 * 可视化日历程序 10 * 11 * @author HQ 12 * @e-mail [email protected] 13 * @date 2018/10/11. 14 */ 15 public class TestCalendar2 { 16 public static void main(String[] args) throws ParseException { 17 String string = "2018-10-11"; 18 19 20 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 21 Date date = dateFormat.parse(string); 22 Calendar calendar = new GregorianCalendar(); 23 calendar.setTime(date); 24 int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 25 int day = calendar.get(Calendar.DAY_OF_MONTH); 26 27 System.out.println("日\t一\t二\t三\t四\t五\t六"); 28 calendar.set(Calendar.DAY_OF_MONTH, 1); 29 30 for (int i = 0; i < calendar.get(Calendar.DAY_OF_WEEK) - 1; i++) { 31 System.out.print("\t"); 32 } 33 34 for (int i = 1; i <= days; i++) { 35 //判断是否为输入的时间是否为今天 36 if(day==calendar.get(Calendar.DAY_OF_MONTH)){ 37 //如果是今天的话用红色标记 38 System.out.print("\u001b[31m"+calendar.get(Calendar.DAY_OF_MONTH) + "\t"+"\u001b[0m"); 39 }else{ 40 System.out.print(calendar.get(Calendar.DAY_OF_MONTH) + "\t"); 41 } 42 43 if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { 44 System.out.println(); //换行 45 } 46 calendar.add(Calendar.DAY_OF_MONTH, 1); 47 } 48 } 49 }
运行结果: