第二个Java程序:本月日历的显示

显示本月的日历,每周的开始日期按照标准库中规定的日期,当天右侧用"*"进行标注,代码如下:

 1 package Source1;

 2 

 3 import java.text.DateFormatSymbols;

 4 import java.util.*;

 5 import java.io.*;

 6 import java.awt.*;

 7 import javax.swing.*;

 8 

 9 public class Test2 {

10     public static void main(String[] args){        

11         GregorianCalendar now = new GregorianCalendar();

12         int month = now.get(Calendar.MONTH);

13         int today = now.get(Calendar.DAY_OF_MONTH);

14         int intent = 0;

15         now.set(Calendar.DAY_OF_MONTH,1);

16         int week = now.get(Calendar.DAY_OF_WEEK);

17         int firstDayOfWeek = now.getFirstDayOfWeek();

18         while(firstDayOfWeek != week){

19             ++intent;

20             now.add(Calendar.DAY_OF_MONTH, -1);

21             week = now.get(Calendar.DAY_OF_WEEK);

22         }

23         String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();

24         do{

25             System.out.printf("%4s",weekdayNames[week]);

26             now.add(Calendar.DAY_OF_MONTH, 1);

27             week = now.get(Calendar.DAY_OF_WEEK);

28         }while(week != firstDayOfWeek);

29         System.out.println();

30         for(int i = 0;i < intent;++i){

31             System.out.print("    ");

32         }

33         now.set(Calendar.DAY_OF_MONTH, 1);

34         week = now.get(Calendar.DAY_OF_WEEK);

35         int day = now.get(Calendar.DAY_OF_MONTH);

36         do{

37             System.out.printf("%3s",day);

38             if(today == day){

39                 System.out.print("*");

40             }else{

41                 System.out.print(" ");

42             }

43             now.add(Calendar.DAY_OF_MONTH, 1);

44             week = now.get(Calendar.DAY_OF_WEEK);

45             day = now.get(Calendar.DAY_OF_MONTH);

46             if(week == firstDayOfWeek){

47                 System.out.println();

48             }

49         }while(month == now.get(Calendar.MONTH));

50         if(week != firstDayOfWeek){

51             System.out.println();

52         }

53     }

54 }

运行后截图如下:

第二个Java程序:本月日历的显示

你可能感兴趣的:(java)