1

我需要生成一个整数数组,其中包含需要在当前月份视图中显示的月份中的日期。例如,当前月份视图将在第一行列中显示 8 月 26 日。

在此处输入图像描述

使用 Calendar 和 Date 类在 java 中的上述网格中显示整数的最简单方法是什么?

4

1 回答 1

1

你的问题对我来说很有趣,所以我创建了解决方案:

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class CalendarTable {

    private static List<Integer> daysPositions = new LinkedList<Integer>();

    static {
        daysPositions.add( Calendar.SUNDAY );
        daysPositions.add( Calendar.MONDAY );
        daysPositions.add( Calendar.TUESDAY );
        daysPositions.add( Calendar.WEDNESDAY );
        daysPositions.add( Calendar.THURSDAY );
        daysPositions.add( Calendar.FRIDAY );
        daysPositions.add( Calendar.SATURDAY );
    }

    public static void viewCalendar( Date date ) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( date );

        calendar.set( Calendar.DAY_OF_MONTH, 1 );
        // day of week for first date of month
        int firstDateOfMonthDay = calendar.get( Calendar.DAY_OF_WEEK );
        int weekOfFirstDate = calendar.get( Calendar.WEEK_OF_YEAR );

        int lastDateOfMonth = calendar.getActualMaximum( Calendar.DAY_OF_MONTH );
        calendar.set( Calendar.DAY_OF_MONTH, lastDateOfMonth );
        // day of week for last date of month
        int lastDateOfMonthDay = calendar.get( Calendar.DAY_OF_WEEK );
        int weekOfLastDate = calendar.get( Calendar.WEEK_OF_YEAR );

        calendar.roll( Calendar.MONTH, false );
        int lastDateOfPrevMonth = calendar.getActualMaximum( Calendar.DAY_OF_MONTH );

        int weeksToDisplay = weekOfLastDate - weekOfFirstDate + 1;
        int[] days = new int[weeksToDisplay * 7];

        int firstDayPosition = daysPositions.indexOf( firstDateOfMonthDay );

        // fill previous month
        int x = lastDateOfPrevMonth;
        for ( int i = firstDayPosition - 1; i >= 0; i-- ) {
            days[i] = x--;
        }

        // fill current month
        for ( int i = 1; i < lastDateOfMonth + 1; i++ ) {
            days[firstDayPosition - 1 + i] = i;
        }

        // fill next month
        int j = 1;
        for ( int i = lastDateOfMonth + firstDayPosition; i < days.length; i++ ) {
            days[i] = j++;
        }

        // display calendar
        // ( here you may extract data into your structure )
        for ( int i = 0; i < days.length; i++ ) {
            if ( i % 7 == 0 ) {
                System.out.println();
            }
            System.out.print( days[i] + "\t" );
        }
    }

    public static void main( String[] args ) {
        viewCalendar( new Date() );
    }

}

执行后,您将获得输出(例如 - 当前月份视图):

26  27  28  29  30  31  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  1   2   3   4   5   6

此外,在某些日历中 - 一周的第一天是星期一,因此您可以daysPositions这样填写:

    static {
        daysPositions.add( Calendar.MONDAY );
        daysPositions.add( Calendar.TUESDAY );
        daysPositions.add( Calendar.WEDNESDAY );
        daysPositions.add( Calendar.THURSDAY );
        daysPositions.add( Calendar.FRIDAY );
        daysPositions.add( Calendar.SATURDAY );
        daysPositions.add( Calendar.SUNDAY ); // move Sunday to the end of week
    }

进行此操作后,对于当前月份视图,您将获得:

27  28  29  30  31  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  
1   2   3   4   5   6   7   
于 2012-09-05T10:18:09.863 回答