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:br.inf.ufes.lar.thtbot.utils.TimeUtil.java

License:Open Source License

/**
 * Get the number of minutes elapsed between two Dates.
 *
 * @param startDate Start Date./*from w w w .ja  v  a  2  s.  c  o  m*/
 * @param endDate End Date.
 * @return Number of minutes elapsed between two Dates.
 * @see Date
 * @since 1.0
 */
public static int getMinutesElapsed(Date startDate, Date endDate) {
    Interval interval = new Interval(startDate.getTime(), endDate.getTime());
    Period period = interval.toPeriod();

    return period.getMinutes();
}

From source file:br.inf.ufes.lar.thtbot.utils.TimeUtil.java

License:Open Source License

/**
 * Get the number of seconds elapsed between two Dates.
 *
 * @param startDate Start Date.// w  w w .  j  a  v  a  2  s.  c om
 * @param endDate End Date.
 * @return Number of seconds elapsed between two Dates.
 * @see Date
 * @since 1.0
 */
public static int getSecondsElapsed(Date startDate, Date endDate) {
    Interval interval = new Interval(startDate.getTime(), endDate.getTime());
    Period period = interval.toPeriod();

    return period.getSeconds();
}

From source file:ca.barrenechea.ticker.utils.TimeUtils.java

License:Apache License

public static TimeSpan getSpan(long start, long end) {
    if (end <= start) {
        return new TimeSpan(0, 0, 0);
    }/*from  w  w w. j  ava2  s.  co m*/

    Interval interval = new Interval(start, end);

    Days days = Days.daysIn(interval);
    Hours hours = Hours.hoursIn(interval).minus(days.toStandardHours());
    Minutes minutes = Minutes.minutesIn(interval).minus(days.toStandardMinutes())
            .minus(hours.toStandardMinutes());

    return new TimeSpan(days.getDays(), hours.getHours(), minutes.getMinutes());
}

From source file:ch.bfh.ti.soed.hs16.srs.yellow.data.persistence.BookingEntity.java

License:Open Source License

/**
 * @param startDateTime starting point of te booking in form of JodaTime DateTime type
 * @param endDateTime ending point of the booking in form of JodaTime DateTime type
 *///www.  j  ava 2  s.c  om
public void setInterval(DateTime startDateTime, DateTime endDateTime) {
    this.bookingFromToInterval = new Interval(startDateTime, endDateTime);
}

From source file:colossal.pipe.BaseOptions.java

License:Apache License

public Interval getInterval() {
    if (time != null) {
        String[] parts = time.split("/");
        if (parts.length != 2) {
            System.err.println("Can't parse time: use a format like 2007-03-01T13:00:00Z/2007-03-01T14:00:00Z");
        } else {//w  ww.  j av  a2  s.  co  m
            DateTime s = parseDateTime(parts[0]);
            DateTime e = parseDateTime(parts[1]);
            return new Interval(s, e);
        }
    } else if (start != null) {
        DateTime s = parseDateTime(start);
        if (end != null) {
            DateTime e = parseDateTime(end);
            return new Interval(s, e);
        } else if (duration != null) {
            Period p = parseDuration();
            return new Interval(s, p);
        }
    } else if (end != null && duration != null) {
        DateTime e = parseDateTime(end);
        Period p = parseDuration();
        return new Interval(e.minus(p), p);
    }
    return null;
}

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 . c o  m*/
 * 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.addthis.basis.time.Dates.java

License:Apache License

/**
 * Create an interval from two date strings
 * <p/>/*w w w . j  a  va 2s . c o  m*/
 * begStr | endStr | return
 * -------------------------
 * bad   |  bad   | (now, now)
 * bad   |   E    | (E, E)
 * B    |  bad   | (B, B)
 * B    |   E    | (B, E)
 * B(>E)|   E(<B)| (E, E)
 * <p/>
 * ("bad" in the table above indicates that the input
 * is either null or fails to parse)
 * <p/>
 * TODO: accept default beg/end parameters
 *
 * @param begStr beginning of interval
 * @param endStr end of interval
 * @return
 */
public static Interval parseInterval(String begStr, String endStr, DateTimeFormatter format) {
    DateTime beg = null;
    DateTime end = null;
    boolean begFailed = false;
    boolean endFailed = false;

    try {
        beg = format.parseDateTime(begStr);
    } catch (Exception e) {
        begFailed = true;
    }

    try {
        end = format.parseDateTime(endStr);
    } catch (Exception e) {
        endFailed = true;
    }

    if (begFailed && endFailed) {
        end = beg = new DateTime();
    } else if (begFailed) {
        beg = end;
    } else if (endFailed) {
        end = beg;
    }

    if (beg.isAfter(end)) {
        beg = end;
    }

    return new Interval(beg, end);
}

From source file:com.addthis.hydra.data.filter.value.ValueFilterDateRangeLength.java

License:Apache License

protected Interval parseRange(String rangeString) {
    int sepIndex = rangeString.indexOf(dateSep);
    if (sepIndex <= 0 || sepIndex + 1 == rangeString.length()) {
        throw new IllegalArgumentException("Failed to parse date range: " + rangeString);
    }/*from w w  w  .  j av a 2 s.c  o m*/

    DateTimeFormatter dtf = DateTimeFormat.forPattern(dateFormat);
    DateTime beg = dtf.parseDateTime(rangeString.substring(0, sepIndex));
    DateTime end = dtf.parseDateTime(rangeString.substring(sepIndex + 1, rangeString.length()));

    return new Interval(beg, end);
}

From source file:com.almende.dsol.example.datacenters.FederationModel.java

License:Apache License

/** */
public void newDatacenter() throws Exception {
    final Datacenter dc = new Datacenter(this);

    // setup DC powermix over time
    final int intervalHours = 4;
    for (DateTime t = getDateTime().withTimeAtStartOfDay().plusHours(2); t
            .isBefore(getInterval().getEndMillis()); t = t.plusHours(intervalHours)) {
        final int hour = t.hourOfDay().get();
        final boolean isDayTime = hour > 5 && hour < 18;
        final boolean isExtreme = getRNG().nextBoolean();
        if (isExtreme) {
            final double emissionFactor = isDayTime ? .33 : 1.66;
            dc.newPowermix(new Interval(t, t.plusHours(intervalHours)), emissionFactor);

            // setup EP/DC adaptions for 10% of abnormal/extreme cases
            if (getRNG().nextDouble() < .10) {
                final boolean extraOrSaving = getRNG().nextBoolean();
                final double consumptionFactor = extraOrSaving ? 1.5 : .7;
                dc.newAdaption(new Interval(t.plusHours(1), t.plusHours(3)), consumptionFactor);
            }//from  ww w  . j ava 2  s  . c o m
        }
    }

    // LOG.trace(dc.getName() + " insourceSchedule: "
    // + dc.insourceSchedule.values());
    this.datacenters.put(dc.getName(), dc);
    fireEvent(NEW_DC, dc);
}

From source file:com.almende.eve.agent.MeetingAgent.java

License:Apache License

/**
 * Apply the constraints of the the activity (for example duration)
 * //from  w  ww.ja  v  a  2 s .  c  om
 * @param activity
 * @return changed Returns true if the activity is changed
 */
private boolean applyConstraints() {
    final Activity activity = getState().get("activity", Activity.class);
    boolean changed = false;
    if (activity == null) {
        return false;
    }

    // constraints on attendees/resources
    /*
     * TODO: copy actual attendees to status.attendees
     * List<Attendee> constraintsAttendees =
     * activity.withConstraints().withAttendees();
     * List<Attendee> attendees = new ArrayList<Attendee>();
     * for (Attendee attendee : constraintsAttendees) {
     * attendees.add(attendee.clone());
     * }
     * activity.withStatus().setAttendees(attendees);
     * // TODO: is it needed to check if the attendees are changed?
     */

    // check time constraints
    final Long duration = activity.withConstraints().withTime().getDuration();
    if (duration != null) {
        final String start = activity.withStatus().getStart();
        final String end = activity.withStatus().getEnd();
        if (start != null && end != null) {
            final DateTime startTime = new DateTime(start);
            DateTime endTime = new DateTime(end);
            final Interval interval = new Interval(startTime, endTime);
            if (interval.toDurationMillis() != duration) {
                LOG.info("status did not match constraints. " + "Changed end time to match the duration of "
                        + duration + " ms");

                // duration does not match. adjust the end time
                endTime = startTime.plus(duration);
                activity.withStatus().setEnd(endTime.toString());
                activity.withStatus().setUpdated(DateTime.now().toString());

                changed = true;
            }
        }
    }

    // location constraints
    final String newLocation = activity.withConstraints().withLocation().getSummary();
    final String oldLocation = activity.withStatus().withLocation().getSummary();
    if (newLocation != null && !newLocation.equals(oldLocation)) {
        activity.withStatus().withLocation().setSummary(newLocation);
        changed = true;
    }

    if (changed) {
        // store the updated activity
        getState().put("activity", activity);
    }
    return changed;
}