Example usage for org.joda.time Interval toPeriod

List of usage examples for org.joda.time Interval toPeriod

Introduction

In this page you can find the example usage for org.joda.time Interval toPeriod.

Prototype

public Period toPeriod(PeriodType type) 

Source Link

Document

Converts the duration of the interval to a Period using the specified period type.

Usage

From source file:org.gephi.desktop.timeline.MinimalDrawer.java

License:Open Source License

private void paintUpperRulerForInterval(Graphics2D g2d, DateTime dtFrom, DateTime dtTo) {

    g2d.setFont(settings.graduations.font);
    g2d.setColor(settings.graduations.fontColor);
    int leftMargin = settings.graduations.leftMargin;
    int textTopPosition = settings.graduations.textTopPosition;
    int width = getWidth();
    int height = getHeight();
    // TODO take these from the model

    Interval interval = new Interval(dtFrom, dtTo);

    Period p = interval.toPeriod(PeriodType.days());
    // try to determine length if we had to show milliseconds

    int n = p.getDays();
    int unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("wednesday  ", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("jour");
        for (int i = 0; i < n; i++) {
            g2d.drawString(dtFrom.plusDays(i).dayOfWeek().getAsText(LOCALE), leftMargin + 2 + i * (width / n),
                    textTopPosition);// w w w .ja  v  a  2 s. com
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours());
        }
        return;
    }

    unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("wed ", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("jou");
        for (int i = 0; i < n; i++) {
            g2d.drawString(dtFrom.plusDays(i).dayOfWeek().getAsShortText(LOCALE),
                    leftMargin + 2 + i * (width / n), textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);

            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours());
        }
        return;
    }

    p = interval.toPeriod(PeriodType.days());
    n = p.getDays();
    unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("30", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("j");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getDayOfMonth() + i), leftMargin + 2 + i * (width / n),
                    textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);

            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours());
        }
        return;
    }

    p = interval.toPeriod(PeriodType.months());
    n = p.getMonths();
    unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("September  ", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("mois");
        for (int i = 0; i < n; i++) {
            g2d.drawString(dtFrom.plusMonths(i).monthOfYear().getAsText(LOCALE),
                    leftMargin + 2 + i * (width / n), textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);

            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays());
        }
        return;
    }

    unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("dec ", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("mo");
        for (int i = 0; i < n; i++) {
            g2d.drawString(dtFrom.plusMonths(i).monthOfYear().getAsShortText(LOCALE),
                    leftMargin + 2 + i * (width / n), textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays());
        }
        return;
    }

    unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("29 ", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("m");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getMonthOfYear() + i), leftMargin + 2 + i * (width / n),
                    textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays());
        }
        return;
    }

    p = interval.toPeriod(PeriodType.years());
    n = p.getYears();
    unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("1980 ", null)).getWidth();
    if (n < (width / unitSize)) {
        //System.out.println("year");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getYear() + i), leftMargin + 2 + i * (width / n), textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n),
                    Months.monthsBetween(dtFrom.plusYears(i), dtFrom.plusYears(i + 1)).getMonths());
        }
        return;
    }

    int group = 10;
    n = p.getYears() / group;
    if (n < (width / unitSize)) {
        //System.out.println("10 years");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                    textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months
                    .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
        }
        return;
    }
    group = 20;
    n = p.getYears() / group;
    if (n < (width / unitSize)) {
        //System.out.println("20 years");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                    textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months
                    .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
        }
        return;
    }
    group = 50;
    n = p.getYears() / group;
    if (n < (width / unitSize)) {
        //System.out.println("50 years");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                    textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months
                    .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
        }
        return;
    }
    group = 100;
    n = p.getYears() / group;
    if (n / 100 < (width / unitSize)) {
        //System.out.println("100 years");
        for (int i = 0; i < n; i++) {
            g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                    textTopPosition);
            g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n),
                    height - settings.graduations.textBottomMargin);
            paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months
                    .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
        }
    }
    return;
}

From source file:pt.ist.fenixedu.contracts.tasks.UpdateTeacherAuthorizations.java

License:Open Source License

private Department getDominantDepartment(Person person, ExecutionSemester semester) {
    SortedSet<EmployeeContract> contracts = new TreeSet<EmployeeContract>(new Comparator<EmployeeContract>() {
        @Override/* w  w w .  j a  v  a 2s . c o m*/
        public int compare(EmployeeContract ec1, EmployeeContract ec2) {
            int compare = ec1.getBeginDate().compareTo(ec2.getBeginDate());
            return compare == 0 ? ec1.getExternalId().compareTo(ec2.getExternalId()) : compare;
        }
    });
    Interval semesterInterval = semester.getAcademicInterval().toInterval();
    contracts.addAll(((Collection<EmployeeContract>) person
            .getParentAccountabilities(AccountabilityTypeEnum.WORKING_CONTRACT, EmployeeContract.class))
                    .stream()
                    .filter(ec -> ec.belongsToPeriod(semesterInterval.getStart().toYearMonthDay(),
                            semesterInterval.getEnd().toYearMonthDay()))
                    .filter(Objects::nonNull).collect(Collectors.toSet()));

    Department firstDepartmentUnit = null;
    for (EmployeeContract employeeContract : contracts) {
        Department employeeDepartmentUnit = getEmployeeDepartmentUnit(employeeContract.getUnit());
        if (employeeDepartmentUnit != null) {
            Interval contractInterval = new Interval(
                    employeeContract.getBeginDate().toLocalDate().toDateTimeAtStartOfDay(),
                    employeeContract.getEndDate() == null ? new DateTime(Long.MAX_VALUE)
                            : employeeContract.getEndDate().toLocalDate().toDateTimeAtStartOfDay()
                                    .plusMillis(1));
            Interval overlap = semesterInterval.overlap(contractInterval);
            int days = overlap.toPeriod(PeriodType.days()).getDays() + 1;
            if (days > minimumDaysForActivity) {
                return employeeDepartmentUnit;
            }
            if (firstDepartmentUnit == null) {
                firstDepartmentUnit = employeeDepartmentUnit;
            }
        }
    }
    return firstDepartmentUnit;
}

From source file:pt.ist.fenixedu.teacher.domain.credits.util.ReductionServiceBean.java

License:Open Source License

public BigDecimal getTeacherMaxCreditsFromAge() {
    YearMonthDay dateOfBirthYearMonthDay = getTeacherService().getTeacher().getPerson()
            .getDateOfBirthYearMonthDay();
    if (dateOfBirthYearMonthDay != null) {
        Interval interval = new Interval(dateOfBirthYearMonthDay.toLocalDate().toDateTimeAtStartOfDay(),
                getTeacherService().getExecutionPeriod().getEndDateYearMonthDay().plusDays(1).toLocalDate()
                        .toDateTimeAtStartOfDay());
        int age = interval.toPeriod(PeriodType.years()).getYears();
        if (age >= 65) {
            return BigDecimal.ONE;
        }/*from   w  ww  .  java  2 s  .  com*/
    }
    return BigDecimal.ZERO;
}

From source file:pt.ist.fenixedu.teacher.domain.TeacherCredits.java

License:Open Source License

public static double calculateServiceExemptionCredits(Teacher teacher, ExecutionSemester executionSemester) {
    Set<PersonContractSituation> personProfessionalExemptions = PersonContractSituation
            .getValidTeacherServiceExemptions(teacher, executionSemester);
    Interval semesterInterval = new Interval(
            executionSemester.getBeginDateYearMonthDay().toLocalDate().toDateTimeAtStartOfDay(),
            executionSemester.getEndDateYearMonthDay().toLocalDate().toDateTimeAtStartOfDay());
    int lessonsDays = semesterInterval.toPeriod(PeriodType.days()).getDays();

    List<Interval> notYetOverlapedIntervals = new ArrayList<Interval>();
    List<Interval> newIntervals = new ArrayList<Interval>();
    notYetOverlapedIntervals.add(semesterInterval);

    Double mandatoryLessonHours = calculateMandatoryLessonHours(teacher, executionSemester);
    Double maxSneHours = mandatoryLessonHours;
    TeacherService teacherService = TeacherService.getTeacherServiceByExecutionPeriod(teacher,
            executionSemester);/*from www .  j  ava2  s .  com*/
    if (teacherService != null && teacherService.getReductionService() != null) {
        maxSneHours = Math.max(0,
                (mandatoryLessonHours - teacherService.getReductionServiceCredits().doubleValue()));
    }

    for (PersonContractSituation personContractSituation : personProfessionalExemptions) {
        LocalDate exemptionEnd = personContractSituation.getServiceExemptionEndDate() == null
                ? semesterInterval.getEnd().toLocalDate()
                : personContractSituation.getServiceExemptionEndDate();

        Interval exemptionInterval = new Interval(
                personContractSituation.getBeginDate().toDateTimeAtStartOfDay(),
                exemptionEnd.toDateTimeAtStartOfDay());

        PersonProfessionalExemption personProfessionalExemption = personContractSituation
                .getPersonProfessionalExemption();
        if (personContractSituation.countForCredits(semesterInterval)) {
            if (personProfessionalExemption != null) {
                exemptionEnd = personProfessionalExemption.getEndDate() == null
                        ? semesterInterval.getEnd().toLocalDate()
                        : personProfessionalExemption.getEndDate();
                exemptionInterval = new Interval(
                        personProfessionalExemption.getBeginDate().toDateTimeAtStartOfDay(),
                        exemptionEnd.toDateTimeAtStartOfDay());
                if (personProfessionalExemption.getIsSabaticalOrEquivalent()) {
                    if (isSabbaticalForSemester(teacher, exemptionInterval, semesterInterval)) {
                        return maxSneHours;
                    } else {
                        continue;
                    }
                }
            }
            for (Interval notYetOverlapedInterval : notYetOverlapedIntervals) {
                Interval overlapInterval = exemptionInterval.overlap(notYetOverlapedInterval);
                if (overlapInterval != null) {
                    newIntervals.addAll(getNotOverlapedIntervals(overlapInterval, notYetOverlapedInterval));
                } else {
                    newIntervals.add(notYetOverlapedInterval);
                }
            }
            notYetOverlapedIntervals.clear();
            notYetOverlapedIntervals.addAll(newIntervals);
            newIntervals.clear();
        }
    }

    int notOverlapedDays = 0;
    for (Interval interval : notYetOverlapedIntervals) {
        notOverlapedDays += interval.toPeriod(PeriodType.days()).getDays();
    }
    int overlapedDays = lessonsDays - notOverlapedDays;
    Double overlapedPercentage = round(Double.valueOf(overlapedDays) / Double.valueOf(lessonsDays));
    return round(overlapedPercentage * maxSneHours);
}

From source file:pt.ist.fenixedu.teacher.domain.TeacherCredits.java

License:Open Source License

private static double calculateLessonsIntervalAndExemptionOverlapPercentage(Interval lessonsInterval,
        Interval exemptionInterval) {/*from w  ww .  j  a  v  a2  s .  c om*/
    if (lessonsInterval != null) {
        Interval overlapInterval = lessonsInterval.overlap(exemptionInterval);
        if (overlapInterval != null) {
            int intersectedDays = overlapInterval.toPeriod(PeriodType.days()).getDays();
            return round(Double.valueOf(intersectedDays)
                    / Double.valueOf(lessonsInterval.toPeriod(PeriodType.days()).getDays()));
        }
    }
    return 0.0;
}

From source file:TVShowTimelineMaker.timeConstraints.IntervalRelation.java

public void CalculateInter() {
    boolean needToRebuild = false;
    for (OnceDayEvent curEvent : this.ConstrainedEvents) {
        if (curEvent.getLastmodifyed() > this.lastmodifyed) {
            needToRebuild = true;/*from ww w .j  av a  2 s . c om*/
            this.lastmodifyed = Math.max(this.lastmodifyed, curEvent.getLastmodifyed());
        }
    }
    if (needToRebuild) {

        if (this.kind == IntervalRelationKind.LESS_THAN_OR_EQUAL) {
            Interval SecondPairInterval = new Interval(this.SecondPairOne.getEarliestPossibleDate(),
                    this.SecondPairTwo.getLatestPossibleDate());
            this.maxDiff = SecondPairInterval.toPeriod(PeriodType.days()).getDays();
        } else if (this.kind == IntervalRelationKind.EQUAL) {
            if (this.SecondPairOne.getLatestPossibleDate()
                    .isBefore(this.SecondPairTwo.getEarliestPossibleDate())) {
                Interval SecondPairInterval = new Interval(this.SecondPairOne.getLatestPossibleDate(),
                        this.SecondPairTwo.getEarliestPossibleDate());
                this.minDiff = SecondPairInterval.toPeriod(PeriodType.days()).getDays();
            } else {
                this.minDiff = 0;
            }
        } else if (this.kind == IntervalRelationKind.MORE_THAN_OR_EQUAL) {
            Interval FirstPairIntervalMax = new Interval(this.FirstPairOne.getEarliestPossibleDate(),
                    this.FirstPairTwo.getLatestPossibleDate());
            Interval SecondPairIntervalMax = new Interval(this.SecondPairOne.getEarliestPossibleDate(),
                    this.SecondPairTwo.getLatestPossibleDate());
            this.maxDiff = Math.max(FirstPairIntervalMax.toPeriod(PeriodType.days()).getDays(),
                    SecondPairIntervalMax.toPeriod(PeriodType.days()).getDays());
            this.minDiff = 0;
            if (this.FirstPairOne.getLatestPossibleDate()
                    .isBefore(this.FirstPairTwo.getEarliestPossibleDate())) {
                Interval FirstPairIntervalMin = new Interval(this.FirstPairOne.getLatestPossibleDate(),
                        this.FirstPairTwo.getEarliestPossibleDate());
                this.minDiff = Math.max(this.minDiff,
                        FirstPairIntervalMin.toPeriod(PeriodType.days()).getDays());
            } else {
                this.minDiff = 0;
            }
            if (this.SecondPairOne.getLatestPossibleDate()
                    .isBefore(this.SecondPairTwo.getEarliestPossibleDate())) {
                Interval SecondPairIntervalMin = new Interval(this.SecondPairOne.getLatestPossibleDate(),
                        this.SecondPairTwo.getEarliestPossibleDate());
                this.minDiff = Math.max(this.minDiff,
                        SecondPairIntervalMin.toPeriod(PeriodType.days()).getDays());
            } else {
                this.minDiff = 0;
            }
        }
    }
}

From source file:TVShowTimelineMaker.timeline.OnceDayEvent.java

@Override
public int size() {
    if (this.isMarkedForComplexEval()) {
        return this.PossibleDays.size();
    } else if (this.latestPossibleDate.isAfter(this.earliestPossibleDate)) {
        org.joda.time.Interval newInterval = new org.joda.time.Interval(this.earliestPossibleDate,
                this.latestPossibleDate);
        Period daysPeriod = newInterval.toPeriod(org.joda.time.PeriodType.days());
        return daysPeriod.getDays() + 1;
    } else {/*from ww  w. jav a  2 s  .  c  o m*/
        return -1;
    }
}

From source file:TVShowTimelineMaker.timeline.OncePeriodEvent.java

@Override
public boolean containsCould(DayOfYear inDateTime) {
    int monthOfYear = inDateTime.getMonth();
    boolean rAccept;
    org.joda.time.Interval newInterval = this.toLongInterval();
    Period daysPeriod = newInterval.toPeriod(org.joda.time.PeriodType.days());
    if (daysPeriod.getDays() < 366) {
        boolean overYearBound = this.earliestPossibleDateForStart.getYear() < this.latestPossibleDateForEnd
                .getYear();//  w w  w  . j a va  2 s . c  o m
        int startMonth = this.earliestPossibleDateForStart.getMonthOfYear();
        int endMonth = this.latestPossibleDateForEnd.getMonthOfYear();
        int startDay = this.earliestPossibleDateForStart.getDayOfMonth();
        int endDay = this.latestPossibleDateForEnd.getDayOfMonth();
        if ((!overYearBound) && (monthOfYear > startMonth) && (monthOfYear < endMonth)) {
            rAccept = true;
        } else if ((overYearBound) && ((monthOfYear > startMonth) || (monthOfYear < endMonth))) {
            rAccept = true;
        } else if ((monthOfYear == startMonth) && (inDateTime.getDay() >= startDay)) {
            rAccept = true;
        } else if ((monthOfYear == endMonth) && (inDateTime.getDay() <= endDay)) {
            rAccept = true;
        } else {
            rAccept = false;
        }
    } else {
        rAccept = true;
    }
    return rAccept;
}

From source file:TVShowTimelineMaker.timeline.OncePeriodEvent.java

@Override
public boolean containsSure(DayOfYear inDateTime) {
    int monthOfYear = inDateTime.getMonth();
    boolean rAccept;
    org.joda.time.Interval newInterval = this.toShortInterval();
    Period daysPeriod = newInterval.toPeriod(org.joda.time.PeriodType.days());
    if (daysPeriod.getDays() < 366) {
        boolean overYearBound = this.earliestPossibleDateForStart.getYear() < this.latestPossibleDateForEnd
                .getYear();//from  ww  w .j a  va2 s  .c o  m
        int startMonth = this.earliestPossibleDateForStart.getMonthOfYear();
        int endMonth = this.latestPossibleDateForEnd.getMonthOfYear();
        int startDay = this.earliestPossibleDateForStart.getDayOfMonth();
        int endDay = this.latestPossibleDateForEnd.getDayOfMonth();
        if ((!overYearBound) && (monthOfYear > startMonth) && (monthOfYear < endMonth)) {
            rAccept = true;
        } else if ((overYearBound) && ((monthOfYear > startMonth) || (monthOfYear < endMonth))) {
            rAccept = true;
        } else if ((monthOfYear == startMonth) && (inDateTime.getDay() >= startDay)) {
            rAccept = true;
        } else if ((monthOfYear == endMonth) && (inDateTime.getDay() <= endDay)) {
            rAccept = true;
        } else {
            rAccept = false;
        }
    } else {
        rAccept = true;
    }
    return rAccept;
}

From source file:TVShowTimelineMaker.timeline.OncePeriodEvent.java

@Override
public int size() {
    //TODO: refine
    int tSize;//from  w w  w  .ja  v a  2  s  .  c o  m
    if (this.isMarkedForComplexEval()) {
        tSize = this.endPossibleDays.size() * this.startPossibleDays.size();
    } else {
        org.joda.time.Interval startInterval = new org.joda.time.Interval(this.earliestPossibleDateForStart,
                this.latestPossibleDateForStart);
        Period startPeriod = startInterval.toPeriod(org.joda.time.PeriodType.days());
        org.joda.time.Interval endInterval = new org.joda.time.Interval(this.earliestPossibleDateForEnd,
                this.latestPossibleDateForEnd);
        Period endPeriod = endInterval.toPeriod(org.joda.time.PeriodType.days());
        tSize = (startPeriod.getDays() + 1) * (endPeriod.getDays() + 1);
    }
    return tSize;
}