/** * @param baseMillis * the starting point * * @return baseMillis - this timespan */ public long pastTimeMillis(long baseMillis) { return baseMillis - getDurationInMilliseconds(); }
/** * @param baseMillis * the starting point * * @return baseMillis - this timespan */ public long pastTimeMillis(long baseMillis) { return baseMillis - getDurationInMilliseconds(); }
/** * @param baseMilliseconds * the starting point to compute the future time * * @return the absolute time in milliseconds (since jan 01 1970) represented * {@code baseMilliseconds} + this timespan */ public long futureTimeMillis(long baseMilliseconds) { return baseMilliseconds + getDurationInMilliseconds(); }
/** * @param baseMilliseconds * the starting point to compute the future time * * @return the absolute time in milliseconds (since jan 01 1970) represented * {@code baseMilliseconds} + this timespan */ public long futureTimeMillis(long baseMilliseconds) { return baseMilliseconds + getDurationInMilliseconds(); }
/** * @param baseDate * base date to offset from * * @return return the date which is baseDate + value of this timespan */ public Date futureDate(Date baseDate) { return new Date(baseDate.getTime() + getDurationInMilliseconds()); }
/** * @param baseDate * base date to offset from * * @return return the date which is baseDate + value of this timespan */ public Date futureDate(Date baseDate) { return new Date(baseDate.getTime() + getDurationInMilliseconds()); }
/** * Shortcut for creating a timespan and then retrieving the value in milliseconds. * * @param timespan * the timespan as a string * * @return the value in milliseconds */ public static long toMilliseconds(String timespan) { return parseTimespan(timespan).getDurationInMilliseconds(); } }
/** * Shortcut for creating a timespan and then retrieving the value in milliseconds. * * @param timespan * the timespan as a string * * @return the value in milliseconds */ public static long toMilliseconds(String timespan) { return parseTimespan(timespan).getDurationInMilliseconds(); } }
/** * Decomposes this timespan as a map for each unit provided. Example: if this timespan represents * 63s and you provide m/s/ms then you will get 3 timespans in the map: one of 1mn, one for 3s * and one for 0ms. * * @param timeUnits * the time units you want to be part of the decomposition. * * @return a map containing an entry for each time unit provided. All others are <code>null</code>. */ public EnumMap<TimeUnit, Timespan> getAsTimespans(EnumSet<TimeUnit> timeUnits) { EnumMap<TimeUnit, Timespan> res = new EnumMap<TimeUnit, Timespan>(TimeUnit.class); long durationInMillis = getDurationInMilliseconds(); for (TimeUnit timeUnit : TIME_UNIT_ORDER) { if (timeUnits.contains(timeUnit)) { Timespan timespan = truncateDurationToUnit(durationInMillis, timeUnit); res.put(timeUnit, timespan); durationInMillis -= timespan.getDurationInMilliseconds(); } } return res; }
/** * Decomposes this timespan as a map for each unit provided. Example: if this timespan represents * 63s and you provide m/s/ms then you will get 3 timespans in the map: one of 1mn, one for 3s * and one for 0ms. * * @param timeUnits * the time units you want to be part of the decomposition. * * @return a map containing an entry for each time unit provided. All others are <code>null</code>. */ public EnumMap<TimeUnit, Timespan> getAsTimespans(EnumSet<TimeUnit> timeUnits) { EnumMap<TimeUnit, Timespan> res = new EnumMap<TimeUnit, Timespan>(TimeUnit.class); long durationInMillis = getDurationInMilliseconds(); for (TimeUnit timeUnit : TIME_UNIT_ORDER) { if (timeUnits.contains(timeUnit)) { Timespan timespan = truncateDurationToUnit(durationInMillis, timeUnit); res.put(timeUnit, timespan); durationInMillis -= timespan.getDurationInMilliseconds(); } } return res; }
/** * @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); }
/** * Truncates this timespan to the given time unit. Example: if this is 1h20m5s then * <code>truncate(TimeUnit.HOUR)</code> will return 1h, * <code>truncate(TimeUnit.MINUTE)</code> will return 1h20m and * <code>truncate(TimeUnit.SECOND)</code> will return 1h20m5s * * @param timeUnit * the unit you want the timespan in * * @return this timespan if time unit matches otherwise a new one with the given unit */ public Timespan truncate(TimeUnit timeUnit) { if (getTimeUnit() == timeUnit) { return this; } return truncateDurationToUnit(getDurationInMilliseconds(), timeUnit); }
/** * 2 timespans can be different (1h and 3600s) while representing the same duration expressed * in milliseconds... This method tests for this. * * @param timespan * the other timespan to compare with * * @return <code>true</code> if this timespan and the provided one represent the same * duration of time. */ public boolean equalsDurationInMilliseconds(Timespan timespan) { // shortcut when time unit are the same if (timespan.getTimeUnit() == getTimeUnit()) { return timespan.getDuration() == getDuration(); } else { return getDurationInMilliseconds() == timespan.getDurationInMilliseconds(); } }
/** * 2 timespans can be different (1h and 3600s) while representing the same duration expressed * in milliseconds... This method tests for this. * * @param timespan * the other timespan to compare with * * @return <code>true</code> if this timespan and the provided one represent the same * duration of time. */ public boolean equalsDurationInMilliseconds(Timespan timespan) { // shortcut when time unit are the same if (timespan.getTimeUnit() == getTimeUnit()) { return timespan.getDuration() == getDuration(); } else { return getDurationInMilliseconds() == timespan.getDurationInMilliseconds(); } }
/** * Truncates this timespan to the given time unit. Example: if this is 1h20m5s then * <code>truncate(TimeUnit.HOUR)</code> will return 1h, * <code>truncate(TimeUnit.MINUTE)</code> will return 1h20m and * <code>truncate(TimeUnit.SECOND)</code> will return 1h20m5s * * @param timeUnit * the unit you want the timespan in * * @return this timespan if time unit matches otherwise a new one with the given unit */ public Timespan truncate(TimeUnit timeUnit) { if (getTimeUnit() == timeUnit) { return this; } return truncateDurationToUnit(getDurationInMilliseconds(), timeUnit); }
/** * 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); }