Example usage for org.joda.time Interval Interval

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

Introduction

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

Prototype

public Interval(Object interval, Chronology chronology) 

Source Link

Document

Constructs a time interval by converting or copying from another object, overriding the chronology.

Usage

From source file:it.mesis.utility.TimeUtil.java

License:Open Source License

/**
 * giorni trascorsi da dateFrom a dateTo, se dateFrom > dateTo il risultato sar negativo.
 * (usa joda-time)//  w w w  .  j  av a 2 s .  co  m
 * @param dateFrom cannot be null
 * @param dateTo cannot be null
 * @return giorni trascorsi
 * @throws IllegalArgumentException 
 */
public static Integer daysPast(Date dateFrom, Date dateTo) throws IllegalArgumentException {

    if (dateFrom == null || dateTo == null)
        throw new IllegalArgumentException("params cannot be null.");

    boolean sign = dateFrom.compareTo(dateTo) < 0;
    Interval interval = sign ? new Interval(dateFrom.getTime(), dateTo.getTime())
            : new Interval(dateTo.getTime(), dateFrom.getTime());

    return interval.toDuration().toStandardDays().getDays() * (sign ? 1 : -1);
}

From source file:it.webappcommon.lib.DateUtils.java

License:Open Source License

/**
 * http://stackoverflow.com/questions/15358409/dividing-a-joda-time-period-
 * into-intervals-of-desired-size//w  w w .  j  av  a 2  s .c o m
 * 
 * @param start
 * @param end
 * @param chunkAmount
 * @param chunkSize
 * @return
 */
public static List<Interval> splitDuration(DateTime start, DateTime end, long chunkAmount, long chunkSize) {
    long millis = start.getMillis();
    List<Interval> list = new ArrayList<Interval>();

    // for(int i = 0; i < chunkAmount; ++i) {
    // list.add(new Interval(millis, millis += chunkSize));
    // }

    while (millis <= end.getMillis()) {
        list.add(new Interval(millis, millis += chunkSize));
    }

    if (millis < end.getMillis())
        list.add(new Interval(millis, end.getMillis()));

    return list;
}

From source file:jp.co.ntt.atrs.app.c0.MemberValidator.java

License:Apache License

/**
 * {@inheritDoc}/*from ww  w. j av a  2 s.  c o  m*/
 */
@Override
public void validate(Object target, Errors errors) {

    IMemberForm form = (IMemberForm) target;

    // ?
    if (!errors.hasFieldErrors("mail") && !errors.hasFieldErrors("reEnterMail")) {
        String mail = form.getMail();
        String reEnterMail = form.getReEnterMail();
        if (!mail.equals(reEnterMail)) {
            // ??????????
            errors.reject(MemberErrorCode.E_AR_C0_5001.code());
        }
    }

    // ??
    if (!errors.hasFieldErrors("tel1") && !errors.hasFieldErrors("tel2")) {

        if (!ValidationUtil.isValidTelNum(form.getTel1(), form.getTel2())) {
            // ?????67?????
            errors.reject(MemberErrorCode.E_AR_C0_5002.code());
        }
    }

    // ?
    if (!errors.hasFieldErrors("dateOfBirth")) {

        DateTime dateOfBirthMin = DateTimeUtil.toDateTime(dateOfBirthMinDate);
        DateTime dateOfBirthMax = dateFactory.newDateTime();
        DateTime dateOfBirth = new DateTime(form.getDateOfBirth());

        Interval interval = new Interval(dateOfBirthMin, dateOfBirthMax);
        if (!interval.contains(dateOfBirth)) {
            // ?(190011????)????
            errors.reject(MemberErrorCode.E_AR_C0_5003.code(),
                    new Object[] { dateOfBirthMinDate, DateTimeUtil.toFormatDateString(dateOfBirthMax) }, "");
        }
    }

}

From source file:jp.co.ntt.atrs.domain.common.masterdata.PeakTimeProvider.java

License:Apache License

/**
 * ?????//from w  ww.  j ava2s.com
 * 
 * @param depDate ?
 * @return ??????null
 */
public PeakTime getPeakTime(Date depDate) {
    Assert.notNull(depDate);

    for (PeakTime peakTime : peakTimeList) {
        Interval peakTimeInterval = new Interval(
                new DateTime(peakTime.getPeakStartDate()).withTimeAtStartOfDay(),
                new DateTime(peakTime.getPeakEndDate()).withTimeAtStartOfDay().plus(1));
        // ?????
        if (peakTimeInterval.contains(depDate.getTime())) {
            return peakTime;
        }
    }
    return null;
}

From source file:jp.co.ntt.atrs.domain.service.b0.TicketSharedServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}//  w  w w  .j a  va2s .  c  o m
 */
@Override
public void validateDepatureDate(Date departureDate) throws BusinessException {
    Assert.notNull(departureDate);

    DateTime sysDateMidnight = dateFactory.newDateTime().withTimeAtStartOfDay();
    DateTime limitDateMidnight = getSearchLimitDate().toDateTimeAtStartOfDay();

    // ????????????
    Interval reservationAvailableInterval = new Interval(sysDateMidnight, limitDateMidnight.plusDays(1));
    if (!reservationAvailableInterval.contains(departureDate.getTime())) {
        throw new AtrsBusinessException(TicketSearchErrorCode.E_AR_B1_2001);
    }
}

From source file:jp.co.ntt.atrs.domain.service.b0.TicketSharedServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}//from  w  w w.  ja v a 2s. c o  m
 */
@Override
public boolean isAvailableFareType(FareType fareType, Date depDate) {
    Assert.notNull(fareType);
    Assert.notNull(depDate);

    DateTime depDateMidnight = new DateTime(depDate).withTimeAtStartOfDay();

    // 
    DateTime rsrvAvailableStartDate = depDateMidnight.minusDays(fareType.getRsrvAvailableStartDayNum());

    // 
    DateTime rsrvAvailableEndDate = depDateMidnight.minusDays(fareType.getRsrvAvailableEndDayNum());

    // ?
    DateTime sysDateMidnight = dateFactory.newDateTime().withTimeAtStartOfDay();

    // ?????????
    return new Interval(rsrvAvailableStartDate, rsrvAvailableEndDate.plusDays(1)).contains(sysDateMidnight);
}

From source file:jp.furplag.util.time.JodaPrettifier.java

License:Apache License

/**
 * Return the prettified String if the period includes specified moment.
 *
 * <pre>//from  w ww . jav  a2s. c  o  m
 * prettify(DateTime.now().minusHours(1), null, null, null, null) = "one hour ago." prettify(DateTime.now(), DateTime.now().plusYears(1), null, null, null) = "one year ago." prettify(DateTime.now().minusHours(1), null, null, null, new Period().withDays(1)) = "one hour ago." prettify(DateTime.now().minusHours(1), null, null, null, new Period().withMinites(10)) =
 * DateTime.now().withZone(DateTimeZone.UTC).minusHours(1).toString(DateTimeFormat.forStyle("-M"))
 *
 * <pre>
 *
 * @param then the datetime object, null means current date-time.
 * @param reference the moment of a starting point ( {@link org.joda.time.ReadableInstant} and {@link Long} specifiable ). Use {@code DateTime.now()} as a start point if {@code reference} is null.
 * @param locale the language for Localization ( {@code String} and {@code Locale} specifiable ). Use ROOT if {@code locale} is null.
 * @param limit if the moment is in the specified period, return prettified String ( {@code Period} and {@code Interval} specifiable ). Prettify all, if null.
 * @return the prettified String if the period includes specified moment. In other situation, return stringified date-time.
 */
public static String prettify(final Object then, final Object reference, final Locale locale,
        final DateTimeZone zone, final Object limit) {
    DateTime temporary = DateTimeUtils.toDT(then, zone, true);
    if (temporary == null)
        return StringUtils.EMPTY;
    DateTime ref = DateTimeUtils.toDT(reference, temporary.getZone(), true);
    if (ref == null)
        return doPrettify(temporary, null, locale);
    if (ref.isEqual(temporary))
        ref = ref.plusMillis(1);
    if (limit == null)
        return doPrettify(temporary, ref, locale);
    Interval limitter = null;
    if (Interval.class.equals(limit))
        limitter = (Interval) limit;
    if (limit instanceof Period) {
        limitter = new Interval(ref.minus((Period) limit), ref.plusMillis(1).plus((Period) limit));
    }
    if (limit instanceof BaseSingleFieldPeriod) {
        limitter = new Interval(ref.minus(new Period(limit)), ref.plusMillis(1).plus(new Period(limit)));
    }
    if (ObjectUtils.isAny(ClassUtils.primitiveToWrapper(limit.getClass()), Double.class, Float.class)) {
        limitter = new Interval(toDT(toAJD(ref) - NumberUtils.valueOf(limit, double.class), ref),
                toDT(toAJD(ref) + NumberUtils.valueOf(limit, double.class), ref));
    } else if (BigDecimal.class.equals(limit.getClass())) {
        if (NumberUtils.compareTo((BigDecimal) limit, NumberUtils.down(limit)) == 0) {
            limitter = new Interval(ref.minusMillis(NumberUtils.valueOf(limit, int.class)),
                    ref.plusMillis(NumberUtils.valueOf(limit, int.class) + 1));
        } else {
            limitter = new Interval(toDT(toAJD(ref) - NumberUtils.valueOf(limit, double.class), ref),
                    toDT(toAJD(ref) + NumberUtils.valueOf(limit, double.class), ref));
        }
    } else if (Number.class.isAssignableFrom(ClassUtils.primitiveToWrapper(limit.getClass()))) {
        limitter = new Interval(ref.minusMillis(NumberUtils.valueOf(limit, int.class)),
                ref.plusMillis(NumberUtils.valueOf(limit, int.class) + 1));
    }
    if (DateTime.class.equals(limit.getClass())) {
        limitter = new Interval(ref.minus(((DateTime) limit).getMillis()),
                ref.plus(((DateTime) limit).getMillis() + 1L));
    }
    if (Boolean.class.equals(limit.getClass())) {
        limitter = new Interval(temporary.minusMillis(1),
                ((Boolean) limit) ? temporary.plusMillis(1) : temporary.minusMillis(1));
    }
    if (limitter == null)
        return doPrettify(temporary, ref, locale);
    if (limitter.contains(temporary))
        return doPrettify(temporary, ref, locale);

    return toDT(temporary, GJChronology.getInstance(temporary.getZone()))
            .toString(DateTimeFormat.forStyle(isToday(temporary, temporary.getZone()) ? "-M" : "MS")
                    .withLocale(locale == null ? Locale.ROOT : locale));
}

From source file:jp.furplag.util.time.lunisolar.LunisolarDateTimeUtils.java

License:Apache License

public static int getDayOfMonth(final double firstDayOfMonth, final double julianDay, DateTimeZone zone) {
    DateTime firstDay = toDT(firstDayOfMonth, zone, true);
    if (firstDay == null)
        throw new IllegalArgumentException("\"firstDayOfMonth\" must NOT be empty.");
    DateTime then = toDT(julianDay, zone).withTimeAtStartOfDay();
    Interval interval = firstDayOfMonth > julianDay ? new Interval(then, firstDay.withTimeAtStartOfDay())
            : new Interval(firstDay.withTimeAtStartOfDay(), then);

    return (int) interval.toDuration().getStandardDays() + 1;
}

From source file:julian.lylly.model.Task.java

public void pause() {
    if (!isActive()) {
        throw new IllegalStateException("already non-active");
    }/*from  w w w.  j a v a 2  s . com*/
    intervals.add(new Interval(starttime, Instant.now()));
    starttime = null;
}

From source file:julian.lylly.model.Task.java

/**
 * behaviour undefined if interval list is unsorted
 * any {@code null} input is interpreted as a non-bound in that direction
 * @param focusStart//from w ww .j  a  va  2 s .  c  o m
 * @param focusEnd
 * @return
 */
public Duration getTimeSpentInInterval(LocalDate focusStart, LocalDate focusEnd) {
    Instant startInst, endInst;
    if (intervals.size() == 0 && !isActive()) { // #
        return Duration.ZERO;
    }
    if (focusStart == null) {
        startInst = intervals.size() == 0 ? starttime // != null because #
                : intervals.get(0).getStart().toInstant();
    } else {
        startInst = focusStart.toDateTimeAtStartOfDay().toInstant();
    }
    if (focusEnd == null) {
        endInst = isActive() ? Instant.now()
                //v intervals.size() != 0 because #
                : intervals.get(intervals.size() - 1).getEnd().toInstant();//TODO
    } else {
        endInst = focusEnd.toDateTimeAtStartOfDay().toInstant();
    }
    Interval focus = new Interval(startInst, endInst);

    return getTimeSpentInInterval(focus);
}