• 第二个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 }

    运行后截图如下:

  • 相关阅读:
    python闭包&深浅拷贝&垃圾回收&with语句
    Python基本数据类型
    面向对象
    四则运算2
    周总结01
    软件工程个人作业01
    java web中乱码的种类和一些解决方式
    java web 增加信息课堂测试00
    课程00作业
    动手动脑07
  • 原文地址:https://www.cnblogs.com/maowang1991/p/2838919.html
Copyright © 2020-2023  润新知