Search in sources :

Example 1 with InvalidCalendarParametersException

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

the class PersianCalendar method fromIso.

@Override
public DateTimeUnit fromIso(DateTimeUnit dateTimeUnit) {
    if (dateTimeUnit.getYear() >= START_PERSIAN.getYear() && dateTimeUnit.getYear() <= STOP_PERSIAN.getYear()) {
        return new DateTimeUnit(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay(), dateTimeUnit.getDayOfWeek(), false);
    }
    if (dateTimeUnit.getYear() < START_ISO.getYear() || dateTimeUnit.getYear() > STOP_ISO.getYear()) {
        throw new InvalidCalendarParametersException("Illegal ISO year, must be between " + START_ISO.getYear() + " and " + STOP_ISO.getYear() + ", was given " + dateTimeUnit.getYear());
    }
    DateTime start = START_ISO.toJodaDateTime();
    DateTime end = dateTimeUnit.toJodaDateTime();
    return plusDays(START_PERSIAN, Days.daysBetween(start, end).getDays());
}
Also used : InvalidCalendarParametersException(org.hisp.dhis.calendar.exception.InvalidCalendarParametersException) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DateTime(org.joda.time.DateTime)

Example 2 with InvalidCalendarParametersException

use of org.hisp.dhis.calendar.exception.InvalidCalendarParametersException 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)

Aggregations

DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)2 InvalidCalendarParametersException (org.hisp.dhis.calendar.exception.InvalidCalendarParametersException)2 DateTime (org.joda.time.DateTime)2