Search in sources :

Example 21 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class I18nFormat method formatPeriod.

/**
 * Formats a period. Returns null if value is null. Returns INVALID_DATE if
 * formatting string is invalid.
 *
 * @param period the value to format.
 */
public String formatPeriod(Period period) {
    if (period == null) {
        return null;
    }
    PeriodType periodType = period.getPeriodType();
    String typeName = periodType.getName();
    if (// Use ISO dates
    periodType instanceof WeeklyAbstractPeriodType) // due to
    // potential week
    // confusion
    {
        DateTime dateTime = new DateTime(period.getStartDate());
        LocalDate date = Instant.ofEpochMilli(period.getStartDate().getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        WeekFields weekFields = WeekFields.of(PeriodType.MAP_WEEK_TYPE.get(periodType.getName()), 4);
        String year = String.valueOf(date.get(weekFields.weekBasedYear()));
        String week = String.valueOf(date.get(weekFields.weekOfWeekBasedYear()));
        if (periodType instanceof WeeklyPeriodType) {
            return String.format("W%s %s", week, year);
        }
        year += dateTime.dayOfWeek().getAsShortText() + " " + year;
        return String.format("W%s %s", week, year);
    } else if (periodType instanceof BiWeeklyAbstractPeriodType) {
        int year;
        int week;
        Calendar calendar = PeriodType.getCalendar();
        BiWeeklyAbstractPeriodType biWeeklyAbstractPeriodType = (BiWeeklyAbstractPeriodType) periodType;
        DateTimeUnit dateTimeUnit = DateTimeUnit.fromJdkDate(period.getStartDate());
        if (calendar.isIso8601()) {
            LocalDate date = LocalDate.of(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay());
            WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 4);
            year = date.get(weekFields.weekBasedYear());
            week = (date.get(weekFields.weekOfWeekBasedYear()) / 2) + 1;
        } else {
            DateTimeUnit date = biWeeklyAbstractPeriodType.adjustToStartOfBiWeek(dateTimeUnit, calendar);
            week = calendar.week(date);
            if (week == 1 && date.getMonth() == calendar.monthsInYear()) {
                date.setYear(date.getYear() + 1);
            }
            year = date.getYear();
        }
        return String.format("BiW%s %s", week, year);
    }
    String keyStartDate = "format." + typeName + ".startDate";
    String keyEndDate = "format." + typeName + ".endDate";
    String startPattern = resourceBundle.getString(keyStartDate);
    String endPattern = resourceBundle.getString(keyEndDate);
    boolean dayPattern = startPattern.contains("dd") || endPattern.contains("dd");
    Date periodStartDate = period.getStartDate();
    Date periodEndDate = period.getEndDate();
    DateTimeUnit start = PeriodType.getCalendar().fromIso(periodStartDate);
    DateTimeUnit end = PeriodType.getCalendar().fromIso(periodEndDate);
    String startDate;
    String endDate;
    if (!dayPattern) {
        // Set day to first of month to not overflow when converting to JDK
        // date
        start.setDay(1);
        end.setDay(1);
        startDate = commonFormatting(new DateTimeUnit(start, true).toJdkDate(), startPattern);
        endDate = commonFormatting(new DateTimeUnit(end, true).toJdkDate(), endPattern);
    } else {
        startDate = PeriodType.getCalendar().formattedDate(startPattern, start);
        endDate = PeriodType.getCalendar().formattedDate(endPattern, end);
    }
    try {
        return Character.toUpperCase(startDate.charAt(0)) + startDate.substring(1) + endDate;
    } catch (IllegalArgumentException ex) {
        return INVALID_DATE;
    }
}
Also used : BiWeeklyAbstractPeriodType(org.hisp.dhis.period.BiWeeklyAbstractPeriodType) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) Calendar(org.hisp.dhis.calendar.Calendar) BiWeeklyAbstractPeriodType(org.hisp.dhis.period.BiWeeklyAbstractPeriodType) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType) LocalDate(java.time.LocalDate) DateTime(org.joda.time.DateTime) BiWeeklyAbstractPeriodType(org.hisp.dhis.period.BiWeeklyAbstractPeriodType) Date(java.util.Date) LocalDate(java.time.LocalDate) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) WeekFields(java.time.temporal.WeekFields)

Example 22 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class BiWeeklyAbstractPeriodType method getRewindedDate.

@Override
public Date getRewindedDate(Date date, Integer rewindedPeriods) {
    Calendar cal = getCalendar();
    Date rewindedDate = date != null ? date : new Date();
    rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(rewindedDate);
    dateTimeUnit = cal.minusWeeks(dateTimeUnit, rewindedPeriods * 2);
    return cal.toIso(dateTimeUnit).toJdkDate();
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 23 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class BiWeeklyAbstractPeriodType method generateRollingPeriods.

/**
 * Generates the last 26 bi-weeks where the last one is the week which the
 * given date is inside.
 */
@Override
public List<Period> generateRollingPeriods(DateTimeUnit end, Calendar calendar) {
    List<Period> periods = Lists.newArrayList();
    DateTimeUnit date = adjustToStartOfBiWeek(end, calendar);
    date = calendar.minusDays(date, 350);
    for (int i = 0; i < 26; i++) {
        periods.add(createPeriod(date, calendar));
        date = calendar.plusWeeks(date, 2);
    }
    return periods;
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 24 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class DailyPeriodType method generateRollingPeriods.

/**
 * Generates the last 365 days where the last one is the day of the given
 * date.
 */
@Override
public List<Period> generateRollingPeriods(DateTimeUnit dateTimeUnit, Calendar calendar) {
    Calendar cal = getCalendar();
    DateTimeUnit iterationDateTimeUnit = cal.minusDays(dateTimeUnit, 364);
    List<Period> periods = Lists.newArrayList();
    for (int i = 0; i < 365; i++) {
        periods.add(createPeriod(iterationDateTimeUnit, calendar));
        iterationDateTimeUnit = cal.plusDays(iterationDateTimeUnit, 1);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 25 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class MonthlyPeriodType method createPeriod.

@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, Calendar calendar) {
    DateTimeUnit start = new DateTimeUnit(dateTimeUnit);
    start.setDay(1);
    DateTimeUnit end = new DateTimeUnit(dateTimeUnit);
    end.setDay(calendar.daysInMonth(end.getYear(), end.getMonth()));
    return toIsoPeriod(start, end, calendar);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Aggregations

DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)98 Calendar (org.hisp.dhis.calendar.Calendar)23 Test (org.junit.jupiter.api.Test)17 Date (java.util.Date)15 DateInterval (org.hisp.dhis.calendar.DateInterval)10 Period (org.hisp.dhis.period.Period)6 DateTime (org.joda.time.DateTime)5 LocalDate (java.time.LocalDate)4 DimensionalObject (org.hisp.dhis.common.DimensionalObject)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 ArrayList (java.util.ArrayList)3 WeekFields (java.time.temporal.WeekFields)2 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 InvalidCalendarParametersException (org.hisp.dhis.calendar.exception.InvalidCalendarParametersException)2 DataDimensionalItemObject (org.hisp.dhis.common.DataDimensionalItemObject)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)2 DateUtils.getMediumDateString (org.hisp.dhis.util.DateUtils.getMediumDateString)2