Java打印本月日历

 1 /**
 2  * Created by GETTING MORE on 2017/6/7.
 3  */
 4 import java.text.DateFormatSymbols;
 5 import java.util.Calendar;
 6 import java.util.GregorianCalendar;
 7 import java.util.Locale;
 8 
 9 public class Date {
10     public static void main(String[] args) {
11         Locale.setDefault(Locale.CHINA);//此行可加也可不加;
12         GregorianCalendar d = new GregorianCalendar();
13         int today = d.get(Calendar.DAY_OF_MONTH);
14         int month = d.get(Calendar.MONTH);  //month的取值从0开始,即比正常值少一,如6月,实际输出为5
15         // int week=d.get(Calendar.DAY_OF_WEEK);
16         //System.out.print(week);
17 
18         d.set(Calendar.DAY_OF_MONTH, 1);
19         //set today is the first day of this month
20         // int Da=d.get(Calendar.DAY_OF_MONTH);
21         // System.out.print(Da);
22         int weekday = d.get(Calendar.DAY_OF_WEEK); //weekday is a number of 1-7,the monday is 2;and the sunday is 1.
23         //System.out.print(weekday);
24         int firstDayOfWeek = d.getFirstDayOfWeek();
25         //System.out.print(firstDayOfWeek);
26         int indent = 0;
27         while (weekday != firstDayOfWeek) {
28             indent++;
29             d.add(Calendar.DAY_OF_MONTH, -1);  //这里起始值是1(因为上面已经把今天设置为本月第一天)
30             // 所以减1后调到上个月最后一天)
31             //int nowday=d.get(Calendar.DAY_OF_MONTH);
32             //  System.out.print(nowday);
33             weekday = d.get(Calendar.DAY_OF_WEEK);
34             //System.out.print(weekday);
35         }
36         //System.out.print(indent);
37         //System.out.print("#"+weekday+"#");
38        //方法1: String[] weekdayNames = {"","Sun","Mon","Tue","Thu","Wed","Fri","Sat"};
39         String[] weekdayNames=new DateFormatSymbols().getShortWeekdays(); //方法2
40         do {
41             System.out.printf("%4s", weekdayNames[weekday]);
42             d.add(Calendar.DAY_OF_MONTH, 1);//这里起始值为上月的28号
43             weekday = d.get(Calendar.DAY_OF_WEEK);
44         }
45         while (weekday != firstDayOfWeek);
46         System.out.println();
47         for (int i = 1; i <= indent; i++)
48             System.out.print("       ");
49         d.add(Calendar.DAY_OF_MONTH, 1);     //这里用DAY_OF_MONTH或DAY_OF_WEEK输出结果一致;
50                   d.set(Calendar.DAY_OF_MONTH,1);
51             do {
52                 int day = d.get(Calendar.DAY_OF_MONTH);
53                 System.out.printf("%4d", day);
54                 if (day == today)System.out.print("*  ");
55                 else System.out.print("   ");
56                 d.add(Calendar.DAY_OF_MONTH, 1);
57                 weekday = d.get(Calendar.DAY_OF_WEEK);
58                 if (weekday == firstDayOfWeek)
59                     System.out.println();
60             }
61             while (d.get(Calendar.MONTH)== month);//因为这里的错误,耽误了大把的时光。错误原因:检查不认真,把Calendar.Month
62         //写成了Calendar.Day_Of_Month,造成出错。
63                if (weekday!=firstDayOfWeek)
64                    System.out.println();
65         }
66 }

posted @ 2017-06-08 19:59  木修儿  阅读(198)  评论(0编辑  收藏  举报