1 package day0603; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.Scanner; 8 9 public class Demo3 { //2015-06-0310 public static void main(String[] args) {11 System.out.print("请输入日期(yyyy-MM-dd):");12 Scanner scan = new Scanner(System.in);13 String str = scan.next();14 System.out.println(" ---------------------------");15 System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六");16 17 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");18 Date date = null;19 try {20 date = simple.parse(str);21 } catch (ParseException e) {22 // TODO Auto-generated catch block23 e.printStackTrace();24 }25 Calendar c = Calendar.getInstance();//日历当前时间,换成自定义时间26 // c.clear();27 c.setTime(date);28 29 int today = c.get(Calendar.DAY_OF_MONTH); //获取当前天数30 31 c.set(Calendar.DAY_OF_MONTH, 1); // 设置当前“天”为1号32 int firstWeekday = c.get(Calendar.DAY_OF_WEEK); // 1号是周几?33 int j = 1;34 while(j < firstWeekday ) {35 System.out.printf("%4s", " ");36 j ++ ;37 }38 int last = c.getActualMaximum(Calendar.DAY_OF_MONTH); //获取本月最大天数 39 40 for(int k = 1;k <= last;k++) {41 if(today == k){ // *号表示今天42 System.out.printf("%4s",("*"+k));43 }else{ 44 System.out.printf("%4s", k);45 }46 if((j+k-1) % 7 == 0)47 System.out.println();48 49 } 50 }51 52 }