Search in sources :

Example 86 with DateTimeUnit

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

the class PersianCalendar method minusMonths.

@Override
public DateTimeUnit minusMonths(DateTimeUnit dateTimeUnit, int months) {
    DateTimeUnit result = new DateTimeUnit(dateTimeUnit);
    int newMonths = months;
    while (newMonths != 0) {
        result.setMonth(result.getMonth() - 1);
        if (result.getMonth() < 1) {
            result.setMonth(monthsInYear());
            result.setYear(result.getYear() - 1);
        }
        newMonths--;
    }
    updateDateUnit(result);
    return result;
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 87 with DateTimeUnit

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

the class PersianCalendar method toWeekIsoInterval.

private DateInterval toWeekIsoInterval(DateTimeUnit dateTimeUnit, int offset, int length) {
    DateTimeUnit from = new DateTimeUnit(dateTimeUnit);
    if (offset > 0) {
        from = plusWeeks(from, offset);
    } else if (offset < 0) {
        from = minusWeeks(from, -offset);
    }
    DateTimeUnit to = new DateTimeUnit(from);
    to = plusWeeks(to, length);
    to = minusDays(to, 1);
    from = toIso(from);
    to = toIso(to);
    return new DateInterval(from, to, DateIntervalType.ISO8601_WEEK);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DateInterval(org.hisp.dhis.calendar.DateInterval)

Example 88 with DateTimeUnit

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

the class PersianCalendar method plusMonths.

@Override
public DateTimeUnit plusMonths(DateTimeUnit dateTimeUnit, int months) {
    if (months < 0) {
        return minusMonths(dateTimeUnit, Math.abs(months));
    }
    DateTimeUnit result = new DateTimeUnit(dateTimeUnit);
    int newMonths = months;
    while (newMonths != 0) {
        result.setMonth(result.getMonth() + 1);
        if (result.getMonth() > monthsInYear()) {
            result.setMonth(1);
            result.setYear(result.getYear() + 1);
        }
        newMonths--;
    }
    updateDateUnit(result);
    return result;
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 89 with DateTimeUnit

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

the class PersianCalendar method toIso.

@Override
public DateTimeUnit toIso(DateTimeUnit dateTimeUnit) {
    if (dateTimeUnit.getYear() >= START_ISO.getYear() && dateTimeUnit.getYear() <= STOP_ISO.getYear()) {
        return new DateTimeUnit(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay(), dateTimeUnit.getDayOfWeek(), true);
    }
    if (dateTimeUnit.getYear() > STOP_PERSIAN.getYear() || dateTimeUnit.getYear() < START_PERSIAN.getYear()) {
        throw new InvalidCalendarParametersException("Illegal PERSIAN year, must be between " + START_PERSIAN.getYear() + " and " + STOP_PERSIAN.getYear() + ", was given " + dateTimeUnit.getYear());
    }
    DateTime dateTime = START_ISO.toJodaDateTime();
    int totalDays = 0;
    for (int year = START_PERSIAN.getYear(); year < dateTimeUnit.getYear(); year++) {
        totalDays += getYearTotal(year);
    }
    for (int month = START_PERSIAN.getMonth(); month < dateTimeUnit.getMonth(); month++) {
        totalDays += getDaysFromMap(dateTimeUnit.getYear(), month);
    }
    totalDays += dateTimeUnit.getDay() - START_PERSIAN.getDay();
    dateTime = dateTime.plusDays(totalDays);
    return new DateTimeUnit(DateTimeUnit.fromJodaDateTime(dateTime), true);
}
Also used : InvalidCalendarParametersException(org.hisp.dhis.calendar.exception.InvalidCalendarParametersException) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DateTime(org.joda.time.DateTime)

Example 90 with DateTimeUnit

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

the class PersianCalendar method toMonthIsoInterval.

private DateInterval toMonthIsoInterval(DateTimeUnit dateTimeUnit, int offset, int length) {
    DateTimeUnit from = new DateTimeUnit(dateTimeUnit);
    if (offset > 0) {
        from = plusMonths(from, offset);
    } else if (offset < 0) {
        from = minusMonths(from, -offset);
    }
    DateTimeUnit to = new DateTimeUnit(from);
    to = plusMonths(to, length);
    to = minusDays(to, 1);
    from = toIso(from);
    to = toIso(to);
    return new DateInterval(from, to, DateIntervalType.ISO8601_MONTH);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DateInterval(org.hisp.dhis.calendar.DateInterval)

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