如何以日历格式打印数组中的值?

How to print values from an array in a calendar format?

所以我有两个月的数组来存储事件时间、日期和优先级:

Events[] september= new Events[30];
Events[] october = new Events[31];

我想在其中插入我的日期数组并使它们以特定格式显示。

 static String[] days = {"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"};

如何获得这样的输出?我似乎找不到路:

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

我为它写了两个不同的代码 我的代码是:

public static void DisplayEvents(Events[] event,int firstWeekday) {
    System.out.println("Sun Mon Tue Wed Thu Fri Sat");
    for(int i =0; i<days.length;i++) {
        System.out.println(String.format("%1s", days[i]) +"");

    }

public static void DisplayEvents(Events[] event,int firstWeekday) {
    int weekDayIndex =0;

    System.out.println("Sun Mon Tue Wed Thu Fri Sat");
    for(int day=1; day<days.length; day++){
        System.out.println(" ");
        weekDayIndex++;
    }
    for(int day =1; day<event.length;day++){
        System.out.print(String.format("%1s", day));
        weekDayIndex++;
        if (weekDayIndex== 7){
            weekDayIndex = 0;
            System.out.println();
        } else {
            System.out.println("");
        }
    }

}

这些代码给我的输出是:

更新后的输出:

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

星号表示举办活动的日期。我已经编写了一个代码来放入随机事件并且我有我的星号。我的问题只是以日历格式打印它并能够在有事件的日子上放置我的星号。任何 contribution/help 将不胜感激。如果需要,我可以提供更多信息。

在这一行

System.out.println(String.format("%1s", day));

使用 print() 而不是 println()

从概念上讲,您希望将每一行构建为单个 String 或输出,在打印新行之前填充所有列。

"main" 原因是在您的 for(int day =1; day<event.length;day++){ 循环中使用了 System.out.println(String.format("%1s", day));,它每天都在换行打印。

现在,您可以通过多种方式实现这一目标,例如,您可以使用 System.out.print 而不是 System.out.println,或者您可以对所有 7 天使用预定义的格式("%2s%1s %2s%1s %2s%1s %2s%1s %2s%1s %2s%1s %2s%1s") 并填写

或者您可以使用类似 StringBuilder 的东西,它可以表示一行,生成给定日期的输出并将其附加到 StringBuilder,例如...

LocalDate date = LocalDate.of(2015, Month.OCTOBER, 1);
int month = date.getMonthValue();
LocalDate endOfMonth = date.withDayOfMonth(date.lengthOfMonth()).plusDays(1);
while (date.getDayOfWeek() != DayOfWeek.SUNDAY) {
    date = date.minusDays(1);
}

String format = "%2s%1s ";
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
while (date.isBefore(endOfMonth)) {

    StringBuilder row = new StringBuilder(7 * 4);
    for (int index = 0; index < 7; index++) {
        if (month == date.getMonthValue()) {
            row.append(String.format(format, Integer.toString(date.getDayOfMonth()), ""));
        } else {
            row.append(String.format(format, "", ""));
        }
        date = date.plusDays(1);
    }
    System.out.println(row);

}

打印....

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  

因为我有太多的烦恼,11 月...

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   

现在,显然,我还没有实现你的 "event" 逻辑,但我在格式中保留了 space(我只是在示例中将其留空)

你打印出来headersok。下一步打印出空格。

int currentWeekDay = 0;
for(int i = 0; i<firstWeekDay; i++){
    System.out.print("    ");
    currentWeekDay++;
    //just in case the firstWeekDay is invalid.
    if(currentWeekDay==7){
        currentWeekDay=0;
        System.out.println();
    }
}

现在打印出数字,每次都加一个硬return。

for(int i = 0; i<event.length; i++){
    System.out.printf("%4s", event[i]);
    currentWeekDay++;
    if(currentWeekDay==7){
        currentWeekDay=0;
        System.out.println();
    }
}

请注意,我使用的是您创建的字符串数组,而不是格式化您正在迭代的索引。

public static void DisplayEvents(Events[] event, int firstWeekday) {
       int weekDayIndex =0;
       System.out.println("Sun Mon Tue Wed Thu Fri Sat");
        System.out.print("1");
        weekDayIndex++;
        for(int day = 2; day < event.length;day++){
            if(weekDayIndex== 7){
                weekDayIndex = 0;
                 System.out.println();
                System.out.print(day);
                weekDayIndex++;
            } else {
                System.out.print(String.format("%1s", day));
                weekDayIndex++;
            }
        }

    }

它打印:

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