【问题标题】:How to calculate a school year?如何计算一个学年?
【发布时间】:2014-08-20 09:06:27
【问题描述】:

我正在尝试使用此日历库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 月”。我不知道如果我解释清楚,每个月的间隔都是一样的,年份必须改变,取决于你打开应用程序的月份。

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

谢谢。

【问题讨论】:

    标签: android date calendar gregorian-calendar


    【解决方案1】:

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

    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
    }
    

    【讨论】:

      【解决方案2】:

      感谢 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);
              }
      

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

      【讨论】:

        猜你喜欢
        • 2010-10-01
        • 2017-04-21
        • 2013-05-16
        • 2010-09-18
        • 2019-05-20
        • 1970-01-01
        • 2021-05-23
        • 2022-11-08
        • 1970-01-01
        相关资源
        最近更新 更多