List of usage examples for org.joda.time Period getYears
public int getYears()
From source file:org.fenixedu.academic.domain.accounting.installments.InstallmentForFirstTimeStudents.java
License:Open Source License
private int getNumberOfMonthsToChargePenalty(final Event event, final DateTime when) { final Period period = new Period(getWhenStartToApplyPenalty(event, when), when.toDateMidnight()); final int numberOfMonths = (period.getYears() * 12) + (period.getMonths() + 1); if (getMaxMonthsToApplyPenalty() == null) { return numberOfMonths; } else {//from w w w . j a va 2 s. co m return numberOfMonths < getMaxMonthsToApplyPenalty() ? numberOfMonths : getMaxMonthsToApplyPenalty(); } }
From source file:org.fenixedu.academic.domain.accounting.installments.InstallmentWithMonthlyPenalty.java
License:Open Source License
protected int getNumberOfMonthsToChargePenalty(DateTime when) { final Period period = new Period(getWhenStartToApplyPenalty().withDayOfMonth(1).toDateMidnight(), when.toDateMidnight());// w w w . ja v a 2s . c om final int numberOfMonths = (period.getYears() * 12) + (period.getMonths() + 1); if (getMaxMonthsToApplyPenalty() == null) { return numberOfMonths; } else { return numberOfMonths < getMaxMonthsToApplyPenalty() ? numberOfMonths : getMaxMonthsToApplyPenalty(); } }
From source file:org.gdg.frisbee.android.utils.Utils.java
License:Apache License
public static String toHumanTimePeriod(Context ctx, DateTime start, DateTime end) { String result;/* w w w. jav a 2 s . c om*/ Resources res = ctx.getResources(); Period p = new Period(start, end); if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0 && p.getHours() == 0 && p.getMinutes() == 0) { result = res.getQuantityString(R.plurals.seconds_ago, p.getSeconds(), p.getSeconds()); } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0 && p.getHours() == 0) { result = res.getQuantityString(R.plurals.minutes_ago, p.getMinutes(), p.getMinutes()); } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0) { result = res.getQuantityString(R.plurals.hours_ago, p.getHours(), p.getHours()); } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0) { result = res.getQuantityString(R.plurals.days_ago, p.getDays(), p.getDays()); } else { result = start.toLocalDateTime() .toString(DateTimeFormat.patternForStyle("M-", res.getConfiguration().locale)); } return result; }
From source file:org.gephi.desktop.timeline.DateTick.java
License:Open Source License
public static DateTick create(double min, double max, int width) { DateTime minDate = new DateTime((long) min); DateTime maxDate = new DateTime((long) max); Period period = new Period(minDate, maxDate, PeriodType.yearMonthDayTime()); ;/*from w w w . j a v a 2 s. com*/ int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); int hours = period.getHours(); int minutes = period.getMinutes(); int seconds = period.getSeconds(); //Top type DateTimeFieldType topType; if (years > 0) { topType = DateTimeFieldType.year(); } else if (months > 0) { topType = DateTimeFieldType.monthOfYear(); } else if (days > 0) { topType = DateTimeFieldType.dayOfMonth(); } else if (hours > 0) { topType = DateTimeFieldType.hourOfDay(); } else if (minutes > 0) { topType = DateTimeFieldType.minuteOfHour(); } else if (seconds > 0) { topType = DateTimeFieldType.secondOfMinute(); } else { topType = DateTimeFieldType.millisOfSecond(); } //Bottom type if (topType != DateTimeFieldType.millisOfSecond()) { DateTimeFieldType bottomType; if (topType.equals(DateTimeFieldType.year())) { bottomType = DateTimeFieldType.monthOfYear(); } else if (topType.equals(DateTimeFieldType.monthOfYear())) { bottomType = DateTimeFieldType.dayOfMonth(); } else if (topType.equals(DateTimeFieldType.dayOfMonth())) { bottomType = DateTimeFieldType.hourOfDay(); } else if (topType.equals(DateTimeFieldType.hourOfDay())) { bottomType = DateTimeFieldType.minuteOfHour(); } else if (topType.equals(DateTimeFieldType.minuteOfHour())) { bottomType = DateTimeFieldType.secondOfMinute(); } else { bottomType = DateTimeFieldType.millisOfSecond(); } //Number of ticks Period p = new Period(minDate, maxDate, PeriodType.forFields(new DurationFieldType[] { bottomType.getDurationType() })); int intervals = p.get(bottomType.getDurationType()); if (intervals > 0) { int intervalSize = width / intervals; if (intervalSize >= MIN_PIXELS) { return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType, bottomType }); } } } return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType }); }
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 www .j a v a 2 s. c o m 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.graylog2.indexer.rotation.strategies.TimeBasedRotationStrategy.java
License:Open Source License
/** * Determines the starting point ("anchor") for a period. * * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time. * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute * the first rotation was started./*from w w w . j ava2 s . co m*/ * * This "snapping" is done accordingly with the other parts of a period. * * For highly irregular periods (those that do not have a small zero component) * * @param period the rotation period * @return the anchor DateTime to calculate rotation periods from */ protected static DateTime determineRotationPeriodAnchor(@Nullable DateTime lastAnchor, Period period) { final Period normalized = period.normalizedStandard(); int years = normalized.getYears(); int months = normalized.getMonths(); int weeks = normalized.getWeeks(); int days = normalized.getDays(); int hours = normalized.getHours(); int minutes = normalized.getMinutes(); int seconds = normalized.getSeconds(); if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) { throw new IllegalArgumentException("Invalid rotation period specified"); } // find the largest non-zero stride in the period. that's our anchor type. statement order matters here! DateTimeFieldType largestStrideType = null; if (seconds > 0) largestStrideType = secondOfMinute(); if (minutes > 0) largestStrideType = minuteOfHour(); if (hours > 0) largestStrideType = hourOfDay(); if (days > 0) largestStrideType = dayOfMonth(); if (weeks > 0) largestStrideType = weekOfWeekyear(); if (months > 0) largestStrideType = monthOfYear(); if (years > 0) largestStrideType = year(); if (largestStrideType == null) { throw new IllegalArgumentException("Could not determine rotation stride length."); } final DateTime anchorTime = MoreObjects.firstNonNull(lastAnchor, Tools.nowUTC()); final DateTimeField field = largestStrideType.getField(anchorTime.getChronology()); // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836 int periodValue = normalized.get(largestStrideType.getDurationType()); final long fieldValue = field.roundFloor(anchorTime.getMillis()); final int fieldValueInUnit = field.get(fieldValue); if (periodValue == 0) { // https://github.com/Graylog2/graylog2-server/issues/836 log.warn( "Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!"); periodValue = 1; } final long difference = (fieldValueInUnit % periodValue); final long newValue = field.add(fieldValue, -1 * difference); return new DateTime(newValue, DateTimeZone.UTC); }
From source file:org.graylog2.indexer.rotation.TimeBasedRotationStrategy.java
License:Open Source License
/** * Determines the starting point ("anchor") for a period. * * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time. * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute * the first rotation was started./*from w ww. ja va 2s.co m*/ * * This "snapping" is done accordingly with the other parts of a period. * * For highly irregular periods (those that do not have a small zero component) * * @param period the rotation period * @return the anchor DateTime to calculate rotation periods from */ protected static DateTime determineRotationPeriodAnchor(Period period) { final Period normalized = period.normalizedStandard(); int years = normalized.getYears(); int months = normalized.getMonths(); int weeks = normalized.getWeeks(); int days = normalized.getDays(); int hours = normalized.getHours(); int minutes = normalized.getMinutes(); int seconds = normalized.getSeconds(); if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) { throw new IllegalArgumentException("Invalid rotation period specified"); } // find the largest non-zero stride in the period. that's our anchor type. statement order matters here! DateTimeFieldType largestStrideType = null; if (seconds > 0) largestStrideType = secondOfMinute(); if (minutes > 0) largestStrideType = minuteOfHour(); if (hours > 0) largestStrideType = hourOfDay(); if (days > 0) largestStrideType = dayOfMonth(); if (weeks > 0) largestStrideType = weekOfWeekyear(); if (months > 0) largestStrideType = monthOfYear(); if (years > 0) largestStrideType = year(); if (largestStrideType == null) { throw new IllegalArgumentException("Could not determine rotation stride length."); } final DateTime now = Tools.iso8601(); final DateTimeField field = largestStrideType.getField(now.getChronology()); // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836 int periodValue = normalized.get(largestStrideType.getDurationType()); final long fieldValue = field.roundFloor(now.getMillis()); final int fieldValueInUnit = field.get(fieldValue); if (periodValue == 0) { // https://github.com/Graylog2/graylog2-server/issues/836 log.warn( "Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!"); periodValue = 1; } final long difference = (fieldValueInUnit % periodValue); final long newValue = field.add(fieldValue, -1 * difference); return new DateTime(newValue, DateTimeZone.UTC); }
From source file:org.hawkular.metrics.core.impl.DateTimeService.java
License:Apache License
public DateTime getTimeSlice(DateTime dt, Duration duration) { Period p = duration.toPeriod(); if (p.getYears() != 0) { return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears()); } else if (p.getMonths() != 0) { return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths()); } else if (p.getWeeks() != 0) { return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks()); } else if (p.getDays() != 0) { return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays()); } else if (p.getHours() != 0) { return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours()); } else if (p.getMinutes() != 0) { return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes()); } else if (p.getSeconds() != 0) { return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds()); }/*from w w w . j a v a2 s . c om*/ return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis()); }
From source file:org.hawkular.metrics.datetime.DateTimeService.java
License:Apache License
public static DateTime getTimeSlice(DateTime dt, Duration duration) { Period p = duration.toPeriod(); if (p.getYears() != 0) { return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears()); } else if (p.getMonths() != 0) { return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths()); } else if (p.getWeeks() != 0) { return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks()); } else if (p.getDays() != 0) { return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays()); } else if (p.getHours() != 0) { return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours()); } else if (p.getMinutes() != 0) { return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes()); } else if (p.getSeconds() != 0) { return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds()); }/*from w ww . j a v a 2s .com*/ return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis()); }
From source file:org.hawkular.metrics.tasks.api.AbstractTrigger.java
License:Apache License
protected DateTime getExecutionTime(long time, Duration duration) { DateTime dt = new DateTime(time); Period p = duration.toPeriod(); if (p.getYears() != 0) { return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears()); } else if (p.getMonths() != 0) { return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths()); } else if (p.getWeeks() != 0) { return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks()); } else if (p.getDays() != 0) { return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays()); } else if (p.getHours() != 0) { return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours()); } else if (p.getMinutes() != 0) { return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes()); } else if (p.getSeconds() != 0) { return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds()); }//from w ww . j av a 2 s. c om return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis()); }