Search in sources :

Example 1 with BiWeeklyAbstractPeriodType

use of org.hisp.dhis.period.BiWeeklyAbstractPeriodType 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)

Aggregations

LocalDate (java.time.LocalDate)1 WeekFields (java.time.temporal.WeekFields)1 Date (java.util.Date)1 Calendar (org.hisp.dhis.calendar.Calendar)1 DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)1 BiWeeklyAbstractPeriodType (org.hisp.dhis.period.BiWeeklyAbstractPeriodType)1 PeriodType (org.hisp.dhis.period.PeriodType)1 WeeklyAbstractPeriodType (org.hisp.dhis.period.WeeklyAbstractPeriodType)1 WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)1 DateTime (org.joda.time.DateTime)1