protected Period toIsoPeriod(
      DateTimeUnit start, DateTimeUnit end, org.hisp.dhis.calendar.Calendar calendar) {
    DateTimeUnit from = calendar.toIso(start);
    DateTimeUnit to = calendar.toIso(end);

    return new Period(this, from.toJdkDate(), to.toJdkDate(), getIsoDate(start, calendar));
  }
  /**
   * @param dateInterval DateInterval to create period from
   * @return the period.
   */
  public Period createPeriod(DateInterval dateInterval) {
    if (dateInterval == null || dateInterval.getFrom() == null || dateInterval.getTo() == null) {
      return null;
    }

    org.hisp.dhis.calendar.Calendar cal = getCalendar();

    final DateTimeUnit from = cal.toIso(dateInterval.getFrom());
    final DateTimeUnit to = cal.toIso(dateInterval.getTo());

    return new Period(this, from.toJdkDate(), to.toJdkDate(), getIsoDate(from));
  }
Example #3
0
  /**
   * Generates a list of Periods for the last 5 years. Must be overridden by CalendarPeriodTypes
   * which do not generate periods for the current year only in their implementation of
   * generatePeriods( Date ).
   *
   * @param date the date which touches the time span to generate Periods for.
   * @return a list of Periods for a defined time span.
   */
  public List<Period> generateLast5Years(Date date) {
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
    dateTimeUnit = getCalendar().minusYears(dateTimeUnit, 4);
    List<Period> periods = Lists.newArrayList();

    Calendar calendar = getCalendar();

    for (int i = 0; i < 5; i++) {
      periods.addAll(generatePeriods(dateTimeUnit));
      dateTimeUnit = calendar.plusYears(dateTimeUnit, 1);
    }

    return periods;
  }
  /**
   * Creates a valid Period based on the given date. E.g. the given date is February 10. 2007, a
   * monthly PeriodType should return February 2007. This method is intended for use in situations
   * where a huge number of of periods will be generated and its desirable to re-use the calendar.
   *
   * @param date the date which is contained by the created period.
   * @param calendar the calendar implementation to use.
   * @return the valid Period based on the given date
   */
  public Period createPeriod(final Date date, final org.hisp.dhis.calendar.Calendar calendar) {
    try {
      return PERIOD_CACHE.get(
          getCacheKey(calendar, date),
          () -> createPeriod(calendar.fromIso(DateTimeUnit.fromJdkDate(date)), calendar));
    } catch (ExecutionException ignored) {
    }

    return null;
  }
 private String getCacheKey(org.hisp.dhis.calendar.Calendar calendar, Date date) {
   return calendar.name() + getName() + date.getTime();
 }
 /**
  * Returns an instance of a DateUnit.
  *
  * @param date date of calendar in local calendar
  * @return an instance of a Calendar without any time of day.
  */
 public static DateTimeUnit createLocalDateUnitInstance(
     Date date, org.hisp.dhis.calendar.Calendar calendar) {
   return calendar.fromIso(DateTimeUnit.fromJdkDate(date));
 }
  /**
   * Returns an instance of a Calendar without any time of day, with the current date.
   *
   * @return an instance of a Calendar without any time of day.
   */
  public static Calendar createCalendarInstance() {
    org.hisp.dhis.calendar.Calendar cal = getCalendar();

    return cal.toIso(cal.today()).toJdkCalendar();
  }
  public Period createPeriod(Calendar cal) {
    org.hisp.dhis.calendar.Calendar calendar = getCalendar();

    return createPeriod(calendar.fromIso(DateTimeUnit.fromJdkCalendar(cal)), calendar);
  }