List of usage examples for org.joda.time Interval toPeriod
public Period toPeriod(PeriodType type)
Period
using the specified period type. From source file:com.addthis.basis.time.Dates.java
License:Apache License
/** * Truncate interval start and end by current time * (start/end values after current time are set to current time) * <p/>/* w w w .j a v a2 s . com*/ * Truncate interval start by a specified number of period types * (eg. 30 days, 52 weeks, etc.) * <p/> * If type is null, no truncation is performed. * <p/> * When no truncation is performed, the input interval is returned * (this is useful for efficiently testing if truncation was performed). * * @param interval * @param limit number of TYPE periods above which interval should be truncated * @param type single field period type (the result of this method is undefined for multi-field period types) * @return */ public static Interval truncateInterval(Interval interval, int limit, PeriodType type) { Interval truncatedInterval = interval; if (type != null) { // Truncate end DateTime now = new DateTime(); if (interval.getEnd().isAfter(now)) { if (interval.getStart().isAfter(now)) { truncatedInterval = new Interval(now, now); } else { truncatedInterval = interval.withEnd(now); } } // Truncate start if (truncatedInterval.toPeriod(type).getValue(0) > --limit) { Period limitPeriod = period(limit, type); DateTime truncatedStart = truncatedInterval.getEnd().minus(limitPeriod); truncatedInterval = truncatedInterval.withStart(truncatedStart); } } return truncatedInterval; }
From source file:com.netflix.ice.basic.BasicDataManager.java
License:Apache License
private double[] getData(Interval interval, TagLists tagLists) throws ExecutionException { DateTime start = config.startDate;/*from ww w .j a va 2s.c o m*/ DateTime end = config.startDate; if (consolidateType == ConsolidateType.hourly) { start = interval.getStart().withDayOfMonth(1).withMillisOfDay(0); end = interval.getEnd(); } else if (consolidateType == ConsolidateType.daily) { start = interval.getStart().withDayOfYear(1).withMillisOfDay(0); end = interval.getEnd(); } int num = 0; if (consolidateType == ConsolidateType.hourly) { num = interval.toPeriod(PeriodType.hours()).getHours(); if (interval.getStart().plusHours(num).isBefore(interval.getEnd())) num++; } else if (consolidateType == ConsolidateType.daily) { num = interval.toPeriod(PeriodType.days()).getDays(); if (interval.getStart().plusDays(num).isBefore(interval.getEnd())) num++; } else if (consolidateType == ConsolidateType.weekly) { num = interval.toPeriod(PeriodType.weeks()).getWeeks(); if (interval.getStart().plusWeeks(num).isBefore(interval.getEnd())) num++; } else if (consolidateType == ConsolidateType.monthly) { num = interval.toPeriod(PeriodType.months()).getMonths(); if (interval.getStart().plusMonths(num).isBefore(interval.getEnd())) num++; } double[] result = new double[num]; do { ReadOnlyData data = getReadOnlyData(start); int resultIndex = 0; int fromIndex = 0; if (interval.getStart().isBefore(start)) { if (consolidateType == ConsolidateType.hourly) { resultIndex = Hours.hoursBetween(interval.getStart(), start).getHours(); } else if (consolidateType == ConsolidateType.daily) { resultIndex = Days.daysBetween(interval.getStart(), start).getDays(); } else if (consolidateType == ConsolidateType.weekly) { resultIndex = Weeks.weeksBetween(interval.getStart(), start).getWeeks(); } else if (consolidateType == ConsolidateType.monthly) { resultIndex = Months.monthsBetween(interval.getStart(), start).getMonths(); } } else { if (consolidateType == ConsolidateType.hourly) { fromIndex = Hours.hoursBetween(start, interval.getStart()).getHours(); } else if (consolidateType == ConsolidateType.daily) { fromIndex = Days.daysBetween(start, interval.getStart()).getDays(); } else if (consolidateType == ConsolidateType.weekly) { fromIndex = Weeks.weeksBetween(start, interval.getStart()).getWeeks(); if (start.getDayOfWeek() != interval.getStart().getDayOfWeek()) fromIndex++; } else if (consolidateType == ConsolidateType.monthly) { fromIndex = Months.monthsBetween(start, interval.getStart()).getMonths(); } } List<Integer> columeIndexs = Lists.newArrayList(); int columeIndex = 0; for (TagGroup tagGroup : data.getTagGroups()) { if (tagLists.contains(tagGroup)) columeIndexs.add(columeIndex); columeIndex++; } while (resultIndex < num && fromIndex < data.getNum()) { double[] fromData = data.getData(fromIndex++); for (Integer cIndex : columeIndexs) result[resultIndex] += fromData[cIndex]; resultIndex++; } if (consolidateType == ConsolidateType.hourly) start = start.plusMonths(1); else if (consolidateType == ConsolidateType.daily) start = start.plusYears(1); else break; } while (start.isBefore(end)); return result; }
From source file:com.netflix.ice.basic.BasicThroughputMetricService.java
License:Apache License
public double[] getData(Interval interval, ConsolidateType consolidateType) throws Exception { DateTime start = interval.getStart().withDayOfMonth(1).withMillisOfDay(0); DateTime end = interval.getEnd();/*w ww . ja v a 2 s. co m*/ int num = interval.toPeriod(PeriodType.hours()).getHours(); if (interval.getStart().plusHours(num).isBefore(interval.getEnd())) num++; double[] hourly = new double[num]; List<Double> monthly = Lists.newArrayList(); do { double total = 0; int resultIndex = interval.getStart().isBefore(start) ? Hours.hoursBetween(interval.getStart(), start).getHours() : 0; int fromIndex = interval.getStart().isBefore(start) ? 0 : Hours.hoursBetween(start, interval.getStart()).getHours(); double[] data = this.data.get(start); while (resultIndex < num && fromIndex < data.length) { total += data[fromIndex]; hourly[resultIndex++] = data[fromIndex++]; } start = start.plusMonths(1); monthly.add(total); } while (start.isBefore(end)); int hoursInPeriod = (int) (consolidateType.millis / AwsUtils.hourMillis); num = consolidateType == ConsolidateType.monthly ? monthly.size() : (int) Math.ceil(1.0 * num / hoursInPeriod); double[] result = new double[num]; if (consolidateType == ConsolidateType.monthly) { for (int i = 0; i < num; i++) result[i] = monthly.get(i); } else { for (int i = 0; i < hourly.length; i++) result[i / hoursInPeriod] += hourly[i]; } return result; }
From source file:net.sourceforge.fenixedu.domain.Attends.java
License:Open Source License
public int calculateCurrentWeekOffset() { final DateMidnight beginningOfLessonPeriod = new DateMidnight(getBegginingOfLessonPeriod()); final DateMidnight firstMonday = beginningOfLessonPeriod.withField(DateTimeFieldType.dayOfWeek(), 1); final DateMidnight thisMonday = new DateMidnight().withField(DateTimeFieldType.dayOfWeek(), 1); final Interval interval = new Interval(firstMonday, thisMonday); return interval.toPeriod(PeriodType.weeks()).getWeeks(); }
From source file:net.sourceforge.fenixedu.domain.personnelSection.contracts.PersonContractSituation.java
License:Open Source License
public boolean isLongDuration(Interval interval) { Integer daysBetween = interval.toPeriod(PeriodType.days()).getDays(); return (daysBetween == null || daysBetween >= 90); }
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; }/* ww w . ja va 2s .c o m*/ } return BigDecimal.ZERO; }
From source file:net.sourceforge.fenixedu.domain.Teacher.java
License:Open Source License
public double getServiceExemptionCredits(ExecutionSemester executionSemester) { Set<PersonContractSituation> personProfessionalExemptions = getValidTeacherServiceExemptions( executionSemester);//from ww w .j a v a 2 s. c om 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 = getMandatoryLessonHours(executionSemester); Double maxSneHours = mandatoryLessonHours; TeacherService teacherService = getTeacherServiceByExecutionPeriod(executionSemester); 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(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:net.sourceforge.fenixedu.domain.Teacher.java
License:Open Source License
private double calculateLessonsIntervalAndExemptionOverlapPercentage(Interval lessonsInterval, Interval exemptionInterval) {//from ww w.ja va2 s . c o m 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:org.agatom.springatom.webmvc.converters.IntervalToStringConverter.java
License:Open Source License
/** {@inheritDoc} */ @Override//from www.j a v a 2s .c om public String convert(final Interval source) { Preconditions.checkNotNull(source); return String.valueOf(source.toPeriod(PeriodType.minutes()).getMinutes()); }
From source file:org.alfresco.util.DateUtil.java
License:Open Source License
/** * Calculate the number of days between start and end dates based on the <b>default</b> timezone. * If the end date is before the start date, the returned value is negative. * * @param startMs start date in milliseconds * @param endMs end date in milliseconds * @return number days between//from w ww .j ava2 s . c o m */ public static int calculateDays(long startMs, long endMs) { DateTime startDateTime = new DateTime(startMs).withTimeAtStartOfDay(); DateTime endDateTime = new DateTime(endMs).withTimeAtStartOfDay(); int days; if (endDateTime.isBefore(startDateTime)) { Interval interval = new Interval(endDateTime, startDateTime); Period period = interval.toPeriod(PeriodType.days()); days = 0 - period.getDays(); } else { Interval interval = new Interval(startDateTime, endDateTime); Period period = interval.toPeriod(PeriodType.days()); days = period.getDays(); } return days; }