Example usage for org.joda.time Duration getMillis

List of usage examples for org.joda.time Duration getMillis

Introduction

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

Prototype

public long getMillis() 

Source Link

Document

Gets the length of this duration in milliseconds.

Usage

From source file:io.vitess.client.grpc.GrpcClient.java

License:Apache License

private VitessStub getAsyncStub(Context ctx) {
    Duration timeout = ctx.getTimeout();
    if (timeout == null) {
        return asyncStub;
    }/*from w  w w .  jav  a2s . c o m*/
    return asyncStub.withDeadlineAfter(timeout.getMillis(), TimeUnit.MILLISECONDS);
}

From source file:io.vitess.client.grpc.GrpcClient.java

License:Apache License

private VitessFutureStub getFutureStub(Context ctx) {
    Duration timeout = ctx.getTimeout();
    if (timeout == null) {
        return futureStub;
    }/*from   ww w .  j  a v  a  2  s  .  c o m*/
    return futureStub.withDeadlineAfter(timeout.getMillis(), TimeUnit.MILLISECONDS);
}

From source file:is.illuminati.block.spyros.garmin.model.Speed.java

License:Open Source License

private Speed(final Length length, final Duration duration, final Unit unit) {
    final double seconds = duration.getMillis() * 0.001;

    value = length.getValueInMeters() / seconds;

    this.unit = unit;
}

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

License:Open Source License

/**
 * //  ww w  .ja  va 2  s.  co  m
 * @param _fromDate
 * @param _fromTime
 * @param _toDate
 * @param _toTime
 * @return
 */
public static Double calculateDuration(Date _fromDate, Date _fromTime, Date _toDate, Date _toTime) {
    Double duration;
    Calendar calFromDate = null;
    Calendar calFromTime = null;
    Calendar calToDate = null;
    Calendar calToTime = null;
    Date fromDateTime = null;
    Date toDateTime = null;

    try {
        if ((_fromDate != null) && (_fromTime != null) && (_toDate != null) && (_toTime != null)) {

            logger.debug(String.format("%s=%s", _fromDate, _fromDate.getTime()));
            logger.debug(String.format("%s=%s", _fromTime, _fromTime.getTime()));
            logger.debug(String.format("%s=%s", _toDate, _toDate.getTime()));
            logger.debug(String.format("%s=%s", _toTime, _toTime.getTime()));

            // calFromDate = new GregorianCalendar();
            // calFromDate.setTime(_fromDate);

            // calFromTime = new GregorianCalendar();
            // calFromTime.setTime(_fromTime);

            // calToDate = new GregorianCalendar();
            // calToDate.setTime(_toDate);

            // calToTime = new GregorianCalendar();
            // calToTime.setTime(_toTime);

            // calFromDate.set(Calendar.HOUR_OF_DAY,
            // calFromTime.get(Calendar.HOUR_OF_DAY));
            // calFromDate.set(Calendar.MINUTE,
            // calFromTime.get(Calendar.MINUTE));
            // calFromDate.set(Calendar.SECOND,
            // calFromTime.get(Calendar.SECOND));
            // calFromDate.set(Calendar.MILLISECOND,
            // calFromTime.get(Calendar.MILLISECOND));
            //
            // calToDate.set(Calendar.HOUR_OF_DAY,
            // calToTime.get(Calendar.HOUR_OF_DAY));
            // calToDate.set(Calendar.MINUTE,
            // calToTime.get(Calendar.MINUTE));
            // calToDate.set(Calendar.SECOND,
            // calToTime.get(Calendar.SECOND));
            // calToDate.set(Calendar.MILLISECOND,
            // calToTime.get(Calendar.MILLISECOND));

            // calFromDate.setTime(mergeDateTime(_fromDate, _fromTime));
            // calToDate.setTime(mergeDateTime(_toDate, _toTime));

            // fromDateTime = calFromDate.getTime();
            // toDateTime = calToDate.getTime();

            // logger.debug(String.format("%s=%s", fromDateTime,
            // fromDateTime.getTime()));
            // logger.debug(String.format("%s=%s", toDateTime,
            // fromDateTime.getTime()));

            // duration = ((toDateTime.getTime() - fromDateTime.getTime()) /
            // 1000d / 60d / 60d);

            // DateTime a = new DateTime(mergeDateTime(_fromDate,
            // _fromTime));
            // DateTime b = new DateTime(mergeDateTime(_toDate, _toTime));

            // LocalDateTime a1 = new LocalDateTime(mergeDateTime(_fromDate,
            // _fromTime).getTime());
            // LocalDateTime b1 = new LocalDateTime(mergeDateTime(_toDate,
            // _toTime).getTime());

            // LocalDateTime a1 = mergeLocalDateTime(_fromDate, _fromTime);
            // LocalDateTime b1 = mergeLocalDateTime(_toDate, _toTime);

            DateTime a1 = mergeDateTime2(_fromDate, _fromTime);
            DateTime b1 = mergeDateTime2(_toDate, _toTime);

            // Duration d = new Duration(a, b);
            Duration d1 = new Duration(a1.toDateTime(DateTimeZone.UTC), b1.toDateTime(DateTimeZone.UTC));

            // logger.debug(String.format("%s", duration));

            // logger.debug(d.getMillis() / 1000d / 60d / 60d);
            logger.debug(d1.getMillis() / 1000d / 60d / 60d);

            duration = d1.getMillis() / 1000d / 60d / 60d;

        } else {

            duration = 0d;

        }
    } catch (Exception e) {
        logger.error("calculateDuration", e);
        duration = 0d;
    } finally {
        calFromDate = null;
        calFromTime = null;
        calToDate = null;
        calToTime = null;
        fromDateTime = null;
        toDateTime = null;
    }

    // setDuration(duration);
    return duration;
}

From source file:job.myprojects.Task.java

License:Open Source License

private BigDecimal calcDuration() {
    if (startTime != null && endTime != null && startTime.before(endTime)) {
        Duration duration = new Duration(startTime.getTime(), endTime.getTime());
        return new BigDecimal(duration.getMillis() / (1.0 * DateTimeConstants.MILLIS_PER_HOUR));
    }/*from   w  w w  . j av  a  2  s . c  o  m*/
    return BigDecimal.ZERO;
}

From source file:kr.debop4j.timeperiod.calendars.CalendarDateAdd.java

License:Apache License

/**
 *  ? offset ?  ?? .//from  ww  w.  j a  v a  2s.co m
 *
 * @param start         ?
 * @param offset       
 * @param seekDir      ? 
 * @param seekBoundary   ? 
 * @return  ? ?  ?,  
 */
@Override
protected Pair<DateTime, Duration> calculateEnd(DateTime start, Duration offset, SeekDirection seekDir,
        SeekBoundaryMode seekBoundary) {

    log.trace(
            "? offset ?  ?? ... start=[{}], offset=[{}], seekDir=[{}], seekBoundary=[{}]",
            start, offset, seekDir, seekBoundary);
    Guard.shouldBe(offset.compareTo(ZERO) >= 0, "offset ? 0 ?? ? . offset=[%d]",
            offset.getMillis());

    DateTime end = null;
    DateTime moment = start;
    Duration remaining = offset;

    WeekRange week = new WeekRange(start, getTimeCalendar());

    while (week != null) {
        super.getIncludePeriods().clear();
        super.getIncludePeriods().addAll(getAvailableWeekPeriods(week));

        log.trace(" =[{}]", StringTool.listToString(super.getIncludePeriods()));

        Pair<DateTime, Duration> result = super.calculateEnd(moment, remaining, seekDir, seekBoundary);
        end = result.getV1();
        remaining = result.getV2();

        log.trace("? . end=[{}], remaining=[{}]", end, remaining);

        if (end != null || remaining == null)
            break;

        if (seekDir == SeekDirection.Forward) {
            week = findNextWeek(week);
            if (week != null)
                moment = week.getStart();
        } else {
            week = findPreviousWeek(week);
            if (week != null)
                moment = week.getEnd();
        }
    }

    log.trace(
            "? offset ?  ?? . start=[{}], offset=[{}], seekDir=[{}], seekBoundary=[{}], end=[{}], remaining=[{}]",
            start, offset, seekDir, seekBoundary, end, remaining);

    return Pair.create(end, remaining);
}

From source file:kr.debop4j.timeperiod.calendars.DateAdd.java

License:Apache License

/**
 *  ? offset ?  ?? .//from   www. j ava2  s.c  o m
 *
 * @param start         ?
 * @param offset       
 * @param seekDir      ? 
 * @param seekBoundary   ? 
 * @return ? ?,  
 */
protected Pair<DateTime, Duration> calculateEnd(DateTime start, Duration offset, SeekDirection seekDir,
        SeekBoundaryMode seekBoundary) {
    if (isTraceEnable)
        log.trace(
                "? ?  ?? ... start=[{}], offset=[{}], seekDir=[{}], seekBoundary=[{}]",
                start, offset, seekDir, seekBoundary);
    shouldBe(offset.compareTo(Duration.ZERO) >= 0, "offset? 0 ??? . offset=[%d]",
            offset.getMillis());

    Duration remaining = offset;
    DateTime end;

    // search periods
    ITimePeriodCollection searchPeriods = new TimePeriodCollection(this.includePeriods);
    if (searchPeriods.size() == 0)
        searchPeriods.add(TimeRange.Anytime);

    // available periods
    ITimePeriodCollection availablePeriods = new TimePeriodCollection();
    if (excludePeriods.size() == 0) {
        availablePeriods.addAll(searchPeriods);
    } else {
        if (isTraceEnable)
            log.trace(" ? .");
        TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<>();
        for (ITimePeriod p : searchPeriods) {
            if (excludePeriods.hasOverlapPeriods(p)) {
                if (isTraceEnable)
                    log.trace(" ? ?  ? ");
                for (ITimePeriod gap : gapCalculator.getGaps(excludePeriods, p))
                    availablePeriods.add(gap);
            } else {
                availablePeriods.add(p);
            }
        }
    }

    if (availablePeriods.size() == 0) {
        if (isTraceEnable)
            log.trace(" period   .");
        return Pair.create(null, remaining);
    }

    if (isTraceEnable)
        log.trace("  ? ?   ? ...");
    TimePeriodCombiner periodCombiner = new TimePeriodCombiner<TimeRange>();
    availablePeriods = periodCombiner.combinePeriods(availablePeriods);

    if (isTraceEnable)
        log.trace(" ? .");

    Pair<ITimePeriod, DateTime> result = (seekDir == SeekDirection.Forward)
            ? findNextPeriod(start, availablePeriods)
            : findPrevPeriod(start, availablePeriods);

    ITimePeriod startPeriod = result.getV1();
    DateTime seekMoment = result.getV2();

    //   ?  .
    if (startPeriod == null) {
        if (isTraceEnable)
            log.trace("  ?  .");
        return Pair.create(null, remaining);
    }

    // offset ? 0 ??,  ? ? seekMoment  .
    if (offset.isEqual(Duration.ZERO)) {
        if (isTraceEnable)
            log.trace("offset ? 0?,  ? ? seekMoment .");
        return Pair.create(seekMoment, remaining);
    }

    if (seekDir == SeekDirection.Forward) {

        for (int i = availablePeriods.indexOf(startPeriod); i < availablePeriods.size(); i++) {
            ITimePeriod gap = availablePeriods.get(i);
            Duration gapRemaining = new Duration(seekMoment, gap.getEnd());

            if (isTraceEnable)
                log.trace("Seek forward. gap=[{}], gapRemaining=[{}], remaining=[{}], seekMoment=[{}]", gap,
                        gapRemaining, remaining, seekMoment);

            boolean isTargetPeriod = (seekBoundary == SeekBoundaryMode.Fill)
                    ? gapRemaining.compareTo(remaining) >= 0
                    : gapRemaining.compareTo(remaining) > 0;

            if (isTargetPeriod) {
                end = seekMoment.plus(remaining);
                remaining = null;
                return Pair.create(end, remaining);
            }

            remaining = remaining.minus(gapRemaining);
            if (i == availablePeriods.size() - 1)
                return Pair.create(null, remaining);

            seekMoment = availablePeriods.get(i + 1).getStart(); // next period
        }
    } else {
        for (int i = availablePeriods.indexOf(startPeriod); i >= 0; i--) {
            ITimePeriod gap = availablePeriods.get(i);
            Duration gapRemaining = new Duration(gap.getStart(), seekMoment);

            if (isTraceEnable)
                log.trace("Seek backward. gap=[{}], gapRemaining=[{}], remaining=[{}], seekMoment=[{}]", gap,
                        gapRemaining, remaining, seekMoment);

            boolean isTargetPeriod = (seekBoundary == SeekBoundaryMode.Fill)
                    ? gapRemaining.compareTo(remaining) >= 0
                    : gapRemaining.compareTo(remaining) > 0;

            if (isTargetPeriod) {
                end = seekMoment.minus(remaining);
                remaining = null;
                return Pair.create(end, remaining);
            }
            remaining = remaining.minus(gapRemaining);
            if (i == 0)
                return Pair.create(null, remaining);

            seekMoment = availablePeriods.get(i - 1).getEnd();
        }
    }

    if (isTraceEnable)
        log.trace(" ??  .");
    return Pair.create(null, remaining);
}

From source file:kr.debop4j.timeperiod.TimeBlock.java

License:Apache License

@Override
public ITimeBlock getPreviousBlock(Duration offset) {
    Duration endOffset = (offset.compareTo(ZERO) > 0) ? new Duration(-offset.getMillis()) : offset;
    return new TimeBlock(getDuration(), getStart().plus(endOffset), readonly);
}

From source file:kr.debop4j.timeperiod.TimeBlock.java

License:Apache License

@Override
public ITimeBlock getNextBlock(Duration offset) {
    Duration startOffset = (offset.compareTo(ZERO) > 0) ? offset : new Duration(-offset.getMillis());
    return new TimeBlock(getEnd().plus(startOffset), getDuration(), readonly);
}

From source file:kr.debop4j.timeperiod.Timepart.java

License:Apache License

/**
 * Instantiates a new Timepart./*from  www . j a v  a 2s  .  c o  m*/
 *
 * @param duration the duration
 */
public Timepart(Duration duration) {
    this.timepart = new DateTime(0).withMillisOfDay((int) duration.getMillis());
}