1 package com.test; 2 3 import java.text.DateFormat; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Calendar; 7 import java.util.Date; 8 import java.util.GregorianCalendar; 9 import java.util.Scanner; 10 /** 11 * 可视化日历程序 12 * @author Administrator 13 * 14 */ 15 public class VisualCalendar { 16 public static void main(String[] args){ 17 System.out.print("请输入日期(按照格式:2030-3-10):"); 18 Scanner scanner=new Scanner(System.in); 19 String temp=scanner.nextLine(); 20 DateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 21 try { 22 Date date=df.parse(temp); //字符串转时间 23 Calendar calendar=new GregorianCalendar(); 24 calendar.setTime(date); 25 int day=calendar.get(Calendar.DATE); 26 calendar.set(Calendar.DATE, 1); 27 28 int max=calendar.getActualMaximum(Calendar.DATE);//一个月最多多少天 29 System.out.print("日 一 二 三 四 五 六 "); 30 for(int i=1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){ 31 System.out.print(" "); 32 } 33 34 for(int i=1;i<=max;i++){ 35 if(i==day){ 36 System.out.print("*"); 37 } 38 System.out.print(i+" "); 39 int w=calendar.get(Calendar.DAY_OF_WEEK); 40 if(w==Calendar.SATURDAY) 41 System.out.println(); 42 calendar.add(Calendar.DATE, 1); //每打印一次,日期加一 43 } 44 } catch (ParseException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 49 } 50 }