如何解释这个关于日历的java代码

时间:2011-01-07 23:10:47

标签: java date

我想了解这段代码。我知道这个代码在我输入年,月和日时给了我日历。然后程序显示该月的日历,此外代码现在显示时间和日期。但我想了解它是如何运作的?

import          java.util.*;
import          java.text.SimpleDateFormat;
public class    Calendar3
{
  private static void doSimpleDateFormat() {
    Calendar        now = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
                    System.out.print(" \n It is now : " + formatter.format(now.getTime()));
                    System.out.println();
  }
  public static void CalendarDemo(int Year, int Month, int Date) {
    GregorianCalendar a = new GregorianCalendar();
                    a.set(Calendar.YEAR, Year);
                    a.set(Calendar.MONTH, (Month - 1));
                    a.set(Calendar.DAY_OF_MONTH, 1);
    int             weekday = a.get(Calendar.DAY_OF_WEEK);
    int             month = a.get(Calendar.MONTH);
                    System.out.println("'\n'      YEAR : " + Year);
    switch          (month) {
    case 0:
      System.out.println();
      System.out.println("        JANUARY         ");
      System.out.println();
      break;
    case 1:
      System.out.println();
      System.out.println("        FEBRUARY           ");
      System.out.println();
      break;
    case 2:
      System.out.println();
      System.out.println("         MARCH          ");
      System.out.println();
      break;
    case 3:
      System.out.println();
      System.out.println("         APRIL         ");
      System.out.println();
      break;
    case 4:
      System.out.println();
      System.out.println("          MAY           ");
      System.out.println();
      break;
    case 5:
      System.out.println();
      System.out.println("          JUNE        ");
      System.out.println();
      break;
    case 6:
      System.out.println();
      System.out.println("          JULY          ");
      System.out.println();
      break;
    case 7:
      System.out.println();
      System.out.println("         AUGUST        ");
      System.out.println();
      break;
    case 8:
      System.out.println();
      System.out.println("       SEPTEMBER            ");
      System.out.println();
      break;
    case 9:
      System.out.println();
      System.out.println("        OCTOBER        ");
      System.out.println();
      break;
    case 10:
      System.out.println();
      System.out.println("        NOVEMBER            ");
      System.out.println();
      break;
    case 11:
      System.out.println();
      System.out.println("        DECEMBER           ");
      System.out.println();
      break;
    }
                    System.out.println("Sun Mon Tue Wed Thu Fri Sat");
    for             (int i = Calendar.SUNDAY; i < weekday; i++) {
      System.out.print("  ");
    }
    do {
      int             day = a.get(Calendar.DAY_OF_MONTH);
      if (day < 10) {
        System.out.print(" " + day);
      } else if (day >= 10) {
        System.out.print(day);
      }
      if (day == Date) {
        System.out.print("* ");
      } else {
        System.out.print("  ");
      }
      if (weekday == Calendar.SATURDAY) {
        System.out.println();
      }
      a.add(Calendar.DAY_OF_MONTH, 1);
      weekday = a.get(Calendar.DAY_OF_WEEK);
    }
    while (a.get(Calendar.MONTH) == month);
    if (weekday != Calendar.SUNDAY) {
      System.out.println();
    }
    System.out.println();
    System.out.println("Note: '*' over any number is your desired number");
    System.out.println();
    doSimpleDateFormat();
  }
}

2 个答案:

答案 0 :(得分:3)

这是非常冗长的,写得不是特别好。 (例如,整个开关块可以用一行代码替换)

如果您想了解它的作用以及它是如何做的,我建议您在IDE中的调试器中逐步完成该程序。这将逐行显示所有变量的设置。

编辑:短暂并不总是更好但是因为你想知道你能做多短。

import static java.util.Calendar.*;

private static final SimpleDateFormat YEAR_MONTH  = new SimpleDateFormat("'\n        YEAR: ' yyyy'\n        'MMMM'\n'");
public static void CalendarDemo(int year, int month, int date) {
    GregorianCalendar a = new GregorianCalendar(year, month-1, 1);
    System.out.printf("%n%s%nSun Mon Tue Wed Thu Fri Sat%n%"+4*(SUNDAY-a.get(DAY_OF_WEEK))+"s", YEAR_MONTH.format(a.getTime()).toUpperCase() , "");
    for(;a.get(MONTH) == month-1;a.add(DAY_OF_MONTH, 1)) {
        int day = a.get(DAY_OF_MONTH);
        System.out.printf("%2d%s%s", day, day == date ? "*" : " ", a.get(DAY_OF_WEEK) == SATURDAY ? "\n" : " ");
    }
    System.out.println("\n\nNote: '*' over any number is your desired number\n");
    doSimpleDateFormat();
}

CalendarDemo(2011,01,07);

打印

        YEAR:  2011

        JANUARY

Sun Mon Tue Wed Thu Fri Sat
                         1 
 2   3   4   5   6   7*  8 
 9  10  11  12  13  14  15 
16  17  18  19  20  21  22 
23  24  25  26  27  28  29 
30  31  

答案 1 :(得分:0)

  • 访问www.jetbrains.org,下载intellij社区版

    • 安装它。
    • 创建一个项目并将代码放入其中。

    • 添加主要方法

    public static void main(String [] args){     Calendar3.CalendarDemo(2011,1,8); }

    • 在线上设一个断点: GregorianCalendar a = new GregorianCalendar(); 点击左侧边距。

    • 调试。

BTW:存在格式问题,此更改有助于:

for (int i = Calendar.SUNDAY; i < weekday; i++) {
        System.out.print("    ");
}