/** * Convenient call when providing minutes * * @return the timespan */ public static Timespan minutes(long minutes) { return new Timespan(minutes, TimeUnit.MINUTE); }
/** * Convenient call when providing seconds * * @return the timespan */ public static Timespan seconds(long seconds) { return new Timespan(seconds, TimeUnit.SECOND); }
/** * Convenient call when providing milliseconds * * @return the timespan */ public static Timespan milliseconds(long milliseconds) { return new Timespan(milliseconds); }
/** * Convenient call when providing minutes * * @return the timespan */ public static Timespan minutes(long minutes) { return new Timespan(minutes, TimeUnit.MINUTE); }
/** * Convenient call when providing milliseconds * * @return the timespan */ public static Timespan milliseconds(long milliseconds) { return new Timespan(milliseconds); }
/** * Convenient call when providing seconds * * @return the timespan */ public static Timespan seconds(long seconds) { return new Timespan(seconds, TimeUnit.SECOND); }
/** * @return a (potentially new) version of this timestamp where the unit is * {@link TimeUnit#MILLISECOND}. */ public Timespan toMillisecondsTimespan() { if (getTimeUnit() == TimeUnit.MILLISECOND) { return this; } return new Timespan(getDurationInMilliseconds(), TimeUnit.MILLISECOND); }
/** * @return a (potentially new) version of this timestamp where the unit is * {@link TimeUnit#MILLISECOND}. */ public Timespan toMillisecondsTimespan() { if (getTimeUnit() == TimeUnit.MILLISECOND) { return this; } return new Timespan(getDurationInMilliseconds(), TimeUnit.MILLISECOND); }
/** * Expresses the provided duration in the unit provided. Note that the timespan returned * represent only the truncated version of the duration: if duration is 1002ms and timeunit * is seconds, then the timespan returned is 1 second... leaving behind 2ms. * * @return the timespan */ private static Timespan truncateDurationToUnit(long durationInMillis, TimeUnit timeUnit) { Timespan res; if (durationInMillis >= timeUnit.getMillisecondsCount()) { res = new Timespan(durationInMillis / timeUnit.getMillisecondsCount(), timeUnit); } else { res = ZERO_TIMESPANS.get(timeUnit); } return res; }
/** * Expresses the provided duration in the unit provided. Note that the timespan returned * represent only the truncated version of the duration: if duration is 1002ms and timeunit * is seconds, then the timespan returned is 1 second... leaving behind 2ms. * * @return the timespan */ private static Timespan truncateDurationToUnit(long durationInMillis, TimeUnit timeUnit) { Timespan res; if (durationInMillis >= timeUnit.getMillisecondsCount()) { res = new Timespan(durationInMillis / timeUnit.getMillisecondsCount(), timeUnit); } else { res = ZERO_TIMESPANS.get(timeUnit); } return res; }
try { long duration = Long.parseLong(timespan.substring(startDigitsIdx, i)); timespans[timeUnitOrderIdx++] = new Timespan(duration, timeUnit); startDigitsIdx = i + 1; expectingDigits = true; try { long duration = Long.parseLong(timespan.substring(startDigitsIdx, len)); timespans[timeUnitOrderLen - 1] = new Timespan(duration, TimeUnit.MILLISECOND); count++; } catch (NumberFormatException e) {
try { long duration = Long.parseLong(timespan.substring(startDigitsIdx, i)); timespans[timeUnitOrderIdx++] = new Timespan(duration, timeUnit); startDigitsIdx = i + 1; expectingDigits = true; try { long duration = Long.parseLong(timespan.substring(startDigitsIdx, len)); timespans[timeUnitOrderLen - 1] = new Timespan(duration, TimeUnit.MILLISECOND); count++; } catch (NumberFormatException e) {
/** * Creates and returns a new timespan whose duration is {@code this} * timespan's duration minus the {@code other} timespan's duration. * <p> * The time unit is preserved if {@code other} has the same unit * as {@code this}. * <p> * Negative timespans are not supported, so if the {@code other} * timespan duration is greater than {@code this} timespan duration, * a timespan of zero is returned (i.e., a negative timespan is never * returned). * * @param other * the timespan to subtract from this one * * @return a new timespan representing {@code this - other} */ public Timespan substractWithZeroFloor(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { long delta = Math.max(0, getDuration() - other.getDuration()); return new Timespan(delta, getTimeUnit()); } long delta = Math.max(0, getDurationInMilliseconds() - other.getDurationInMilliseconds()); return new Timespan(delta, TimeUnit.MILLISECOND); }
/** * Creates and returns a new timespan whose duration is {@code this} * timespan's duration minus the {@code other} timespan's duration. * <p> * The time unit is preserved if {@code other} has the same unit * as {@code this}. * <p> * Negative timespans are not supported, so if the {@code other} * timespan duration is greater than {@code this} timespan duration, * a timespan of zero is returned (i.e., a negative timespan is never * returned). * * @param other * the timespan to subtract from this one * * @return a new timespan representing {@code this - other} */ public Timespan substractWithZeroFloor(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { long delta = Math.max(0, getDuration() - other.getDuration()); return new Timespan(delta, getTimeUnit()); } long delta = Math.max(0, getDurationInMilliseconds() - other.getDurationInMilliseconds()); return new Timespan(delta, TimeUnit.MILLISECOND); }
/** * Adds another timespan to this timespan and return a brand new one. Note that the unit is * preserved if <code>other</code> has the same unit as 'this'. * * @param other * the timespan to add * * @return a brand new timespan. */ public Timespan add(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { return new Timespan(getDuration() + other.getDuration(), getTimeUnit()); } return new Timespan(getDurationInMilliseconds() + other.getDurationInMilliseconds(), TimeUnit.MILLISECOND); }
/** * Adds another timespan to this timespan and return a brand new one. Note that the unit is * preserved if <code>other</code> has the same unit as 'this'. * * @param other * the timespan to add * * @return a brand new timespan. */ public Timespan add(Timespan other) { if (getTimeUnit() == other.getTimeUnit()) { return new Timespan(getDuration() + other.getDuration(), getTimeUnit()); } return new Timespan(getDurationInMilliseconds() + other.getDurationInMilliseconds(), TimeUnit.MILLISECOND); }