如何计算学年?

时间:2014-08-20 09:06:28

标签: android date calendar gregorian-calendar

我正在尝试使用此日历库TimesSquare,我需要通过下一个方式启动日历:

Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);

CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
Date today = new Date();
calendar.init(today, nextYear.getTime())
    .withSelectedDate(today);

我的问题是我需要知道一种创建日历的方法,该日历从9月开始,到明年8月结束。

我需要自动执行,我的意思是,如果用户在8月打开应用程序,则日历需要从“2013年9月”到“2014年8月”,但如果用户在9月打开应用程序,日历应为“2014年9月”至“2015年8月”。我不知道如果我解释得很好,每个月的间隔时间都是一样的,年份必须改变,具体取决于您打开应用程序的月份。

如何修改以前的代码来解决这个问题?

谢谢。

2 个答案:

答案 0 :(得分:1)

只需将当前日历的MONTH字段与AUGUST进行比较,然后根据它将init日历进行比较。像这样的Smth:

Calendar today = Calendar.getInstance();
if (today.get(Calendar.MONTH) > Calendar.AUGUST) {
    // set "today" to september of current year and "nextYear" to august of the next one
} else {
    // set "today" to september of previous year and "nextYear" to august of the current one
}

答案 1 :(得分:0)

感谢yunik清醒了我的想法。在那之后,我有了下一个工作的想法。

Calendar today_cal = Calendar.getInstance();
        Calendar start_calendar = Calendar.getInstance();
        Calendar end_calendar = Calendar.getInstance();
        CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
        Date today = new Date();

        if (today_cal.get(Calendar.MONTH) <= Calendar.AUGUST) {
            // set "today" to september of current year and "nextYear" to august of the next one
            int min_year = today_cal.get(Calendar.YEAR) - 1;
            int min_month = Calendar.SEPTEMBER;
            int min_day = 1;
            start_calendar.set(min_year, min_month, min_day);


            int max_year = today_cal.get(Calendar.YEAR);
            int max_month = Calendar.AUGUST;
            end_calendar.set(max_year, max_month, 1);
            int max_day = end_calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            end_calendar.set(max_year, max_month, max_day);

            calendar.init(start_calendar.getTime(), end_calendar.getTime()).withSelectedDate(today);
        } else {
            // set "today" to september of previous year and "nextYear" to august of the current one
            int year = today_cal.get(Calendar.YEAR) + 1;
            int month = Calendar.SEPTEMBER;
            int day = 1;

            // set "today" to september of current year and "nextYear" to august of the next one
            int min_year = today_cal.get(Calendar.YEAR);
            int min_month = Calendar.SEPTEMBER;
            int min_day = 1;
            start_calendar.set(min_year, min_month, min_day);

            int max_year = today_cal.get(Calendar.YEAR) + 1;
            int max_month = Calendar.AUGUST;
            end_calendar.set(max_year, max_month, 1);
            int max_day = end_calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            end_calendar.set(max_year, max_month, max_day);

            calendar.init(start_calendar.getTime(), end_calendar.getTime()).withSelectedDate(today);
        }

我希望这会对某人有所帮助。