Example usage for org.joda.time PeriodType years

List of usage examples for org.joda.time PeriodType years

Introduction

In this page you can find the example usage for org.joda.time PeriodType years.

Prototype

public static PeriodType years() 

Source Link

Document

Gets a type that defines just the years field.

Usage

From source file:com.hmsinc.epicenter.model.health.Interaction.java

License:Open Source License

private int calculateAgeFromDOB(final DateTime dob) {

    int years = -1;
    if (dob != null) {

        Validate.isTrue(dob.isBefore(interactionDate), "Date of birth was in the future: " + dob.toString());
        years = new Period(dob, interactionDate, PeriodType.years()).getYears();
    }//from   ww w . j a  v  a  2s  .  c  om
    return years;
}

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java

License:Open Source License

private IntervalleObject alignPastInterval(IntervalleObject presentInterval, IntervalleObject pastInterval,
        Axis joinAxis) throws ScopeException {

    if (joinAxis != null && presentInterval != null && pastInterval != null) {

        Object lowerPresent = presentInterval.getLowerBound();
        Object lowerPast = pastInterval.getLowerBound();
        Object upperPresent = presentInterval.getUpperBound();
        Object upperPast = pastInterval.getUpperBound();
        ////w  ww.  jav a 2s . co  m
        IDomain image = joinAxis.getDefinition().getImageDomain();
        if (lowerPresent instanceof Date && lowerPast instanceof Date) {

            DateTime lowerPastDT = new DateTime((Date) lowerPast);
            DateTime lowerPresentDT = new DateTime((Date) lowerPresent);
            DateTime upperPresentDT = new DateTime((Date) upperPresent);
            DateTime upperPastDT = new DateTime((Date) upperPast);

            // realign
            if (image.isInstanceOf(IDomain.YEARLY)) {
                // check if present is an exact number of years
                if (lowerPresentDT.getDayOfYear() == 1
                        && upperPresentDT.getDayOfYear() == upperPresentDT.dayOfYear().getMaximumValue()) {
                    // check of both periods have the same number of days
                    Period presentPeriod = new Period(new LocalDate(lowerPresent),
                            (new LocalDate(upperPresent)), PeriodType.days());
                    Period pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)),
                            PeriodType.days());
                    if (presentPeriod.getDays() == pastPeriod.getDays()) {
                        presentPeriod = new Period(new LocalDate(lowerPresent),
                                (new LocalDate(upperPresent)).plusDays(1), PeriodType.years());
                        pastPeriod = new Period(new LocalDate(lowerPast),
                                (new LocalDate(upperPast)).plusDays(1), PeriodType.years());

                        // realign
                        if (presentPeriod.getYears() > pastPeriod.getYears()) {
                            // some days are missing to align the periods
                            if (lowerPastDT.getDayOfYear() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(upperPastDT.getYear(), 1, 1, 0, 0).toDate();
                                return new IntervalleObject(newLowerPast, upperPast);
                            }
                            if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) {
                                // year over year
                                Date newUpperPast = new DateTime(upperPastDT.getYear(), 12, 31, 23, 59)
                                        .toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);
                            }
                        } else {
                            // either already aligned, or some days should
                            // be removed

                            if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) {
                                // year over Year
                                Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59)
                                        .toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);

                            }
                            if (lowerPastDT.getDayOfYear() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0)
                                        .toDate();
                                return new IntervalleObject(newLowerPast, upperPast);
                            }

                        }
                    }
                }
            } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) {
                // check if present is an exact number of month
                if (lowerPresentDT.getDayOfMonth() == 1
                        && upperPresentDT.getDayOfMonth() == upperPresentDT.dayOfMonth().getMaximumValue()) {
                    // check of both periods have the same number of days
                    Period presentPeriod = new Period(new LocalDate(lowerPresent), new LocalDate(upperPresent),
                            PeriodType.days());
                    Period pastPeriod = new Period(new LocalDate(lowerPast), new LocalDate(upperPast),
                            PeriodType.days());
                    if (presentPeriod.getDays() == pastPeriod.getDays()) {
                        // realign
                        presentPeriod = new Period(new LocalDate(lowerPresent),
                                (new LocalDate(upperPresent)).plusDays(1), PeriodType.months());
                        pastPeriod = new Period(new LocalDate(lowerPast),
                                (new LocalDate(upperPast)).plusDays(1), PeriodType.months());
                        if (presentPeriod.getMonths() > pastPeriod.getMonths()) {
                            // some days are missing

                            if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) {
                                // month over month
                                Date newUpperPast = new DateTime(upperPastDT.getYear(),
                                        upperPastDT.getMonthOfYear(),
                                        upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);
                            }

                            if (lowerPastDT.getDayOfMonth() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(lowerPastDT.getYear(),
                                        lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate();
                                return new IntervalleObject(newLowerPast, upperPast);

                            }

                        } else {
                            // either already aligned, of some days should
                            // be removed
                            if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) {
                                /// month over month
                                if (upperPastDT.getMonthOfYear() == 1) {
                                    Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59)
                                            .toDate();
                                    return new IntervalleObject(lowerPast, newUpperPast);

                                } else {

                                    upperPastDT = upperPastDT.minusMonths(1);
                                    Date newUpperPast = new DateTime(upperPastDT.getYear(),
                                            upperPastDT.getMonthOfYear(),
                                            upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate();
                                    return new IntervalleObject(lowerPast, newUpperPast);
                                }
                            }
                            if (lowerPastDT.getDayOfMonth() != 1) {
                                // previous period
                                if (lowerPastDT.getMonthOfYear() == 12) {
                                    Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0)
                                            .toDate();
                                    return new IntervalleObject(newLowerPast, upperPast);

                                } else {
                                    lowerPastDT = lowerPastDT.plusMonths(1);
                                    Date newLowerPast = new DateTime(lowerPastDT.getYear(),
                                            lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate();
                                    return new IntervalleObject(newLowerPast, upperPast);

                                }

                            }

                        }
                    }
                }
            }
        }
    }
    return pastInterval;
}

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java

License:Open Source License

private PeriodType computePeriodType(IDomain image) {
    if (image.isInstanceOf(IDomain.YEARLY)) {
        return PeriodType.years();
    } else if (image.isInstanceOf(IDomain.QUATERLY)) {
        return PeriodType.months();
    } else if (image.isInstanceOf(IDomain.MONTHLY)) {
        return PeriodType.months();
    } else if (image.isInstanceOf(IDomain.WEEKLY)) {
        return PeriodType.weeks();
    } else {//from   w ww.j  a  v  a  2s .  com
        return PeriodType.days();
    }
}

From source file:com.webarch.common.datetime.DateTimeUtils.java

License:Apache License

/**
 * ?/*  www .ja  va  2s . co m*/
 *
 * @param startTime
 * @param endTime
 * @return
 */
public static long getPeriodYears(Date startTime, Date endTime) {
    return getTimePeriod(startTime, endTime, PeriodType.years()).getYears();
}

From source file:net.sourceforge.fenixedu.domain.teacher.ReductionService.java

License:Open Source License

private 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;
        }// w w  w.ja  va2 s. c  o m
    }
    return BigDecimal.ZERO;
}

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);/*from   w  ww. j av a  2s.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:org.jadira.usertype.dateandtime.joda.columnmapper.StringColumnPeriodMapper.java

License:Apache License

private PeriodType determinePeriodType(String s) {

    PeriodType periodType = PeriodType.standard();

    String current = s;//from  ww w.  j  a v  a 2  s  .  c om

    if (current.startsWith(PeriodType.standard().getName())) {
        periodType = PeriodType.standard();
        current = s.substring(PeriodType.standard().getName().length());

    } else if (current.startsWith(PeriodType.yearMonthDayTime().getName())) {
        periodType = PeriodType.yearMonthDayTime();
        current = s.substring(PeriodType.yearMonthDayTime().getName().length());

    } else if (current.startsWith(PeriodType.yearMonthDay().getName())) {
        periodType = PeriodType.yearMonthDay();
        current = s.substring(PeriodType.yearMonthDay().getName().length());

    } else if (current.startsWith(PeriodType.yearWeekDayTime().getName())) {
        periodType = PeriodType.yearWeekDayTime();
        current = s.substring(PeriodType.yearWeekDayTime().getName().length());

    } else if (current.startsWith(PeriodType.yearWeekDay().getName())) {
        periodType = PeriodType.yearWeekDay();
        current = s.substring(PeriodType.yearWeekDay().getName().length());

    } else if (current.startsWith(PeriodType.yearDayTime().getName())) {
        periodType = PeriodType.yearDayTime();
        current = s.substring(PeriodType.yearDayTime().getName().length());

    } else if (current.startsWith(PeriodType.yearDay().getName())) {
        periodType = PeriodType.yearDay();
        current = s.substring(PeriodType.yearDay().getName().length());

    } else if (current.startsWith(PeriodType.dayTime().getName())) {
        periodType = PeriodType.dayTime();
        current = s.substring(PeriodType.dayTime().getName().length());

    } else if (current.startsWith(PeriodType.time().getName())) {
        periodType = PeriodType.time();
        current = s.substring(PeriodType.time().getName().length());

    } else if (current.startsWith(PeriodType.years().getName())) {
        periodType = PeriodType.years();
        current = s.substring(PeriodType.years().getName().length());

    } else if (current.startsWith(PeriodType.months().getName())) {
        periodType = PeriodType.months();
        current = s.substring(PeriodType.months().getName().length());

    } else if (current.startsWith(PeriodType.weeks().getName())) {
        periodType = PeriodType.weeks();
        current = s.substring(PeriodType.weeks().getName().length());

    } else if (current.startsWith(PeriodType.days().getName())) {
        periodType = PeriodType.days();
        current = s.substring(PeriodType.days().getName().length());

    } else if (current.startsWith(PeriodType.hours().getName())) {
        periodType = PeriodType.hours();
        current = s.substring(PeriodType.hours().getName().length());

    } else if (current.startsWith(PeriodType.minutes().getName())) {
        periodType = PeriodType.minutes();
        current = s.substring(PeriodType.minutes().getName().length());

    } else if (current.startsWith(PeriodType.seconds().getName())) {
        periodType = PeriodType.seconds();
        current = s.substring(PeriodType.seconds().getName().length());

    } else if (current.startsWith(PeriodType.millis().getName())) {
        periodType = PeriodType.millis();
        current = s.substring(PeriodType.millis().getName().length());
    }

    while (current.length() > 0) {

        if (current.startsWith("NoYears")) {
            periodType = periodType.withYearsRemoved();
            current = s.substring("NoYears".length());
        } else if (current.startsWith("NoMonths")) {
            periodType = periodType.withMonthsRemoved();
            current = s.substring("NoMonths".length());
        } else if (current.startsWith("NoWeeks")) {
            periodType = periodType.withWeeksRemoved();
            current = s.substring("NoWeeks".length());
        } else if (current.startsWith("NoDays")) {
            periodType = periodType.withDaysRemoved();
            current = s.substring("NoDays".length());
        } else if (current.startsWith("NoHours")) {
            periodType = periodType.withHoursRemoved();
            current = s.substring("NoHours".length());
        } else if (current.startsWith("NoMinutes")) {
            periodType = periodType.withMinutesRemoved();
            current = s.substring("NoMinutes".length());
        } else if (current.startsWith("NoSeconds")) {
            periodType = periodType.withSecondsRemoved();
            current = s.substring("NoSeconds".length());
        } else if (current.startsWith("NoMillis")) {
            periodType = periodType.withMillisRemoved();
            current = s.substring("NoMillis".length());
        } else {
            throw new IllegalArgumentException("Unrecognised PeriodType: " + s + "{" + current + "}");
        }
    }
    return periodType;
}

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  w  w  .  j a  v  a 2 s .co  m
    }
    return BigDecimal.ZERO;
}