/** * @return the duration of this timespan expressed in hours. If m/s/ms are present, * they will be truncated! */ public long getDurationInHours() { return getDuration(TimeUnit.HOUR); }
/** * 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(); } }
/** * @return the duration of this timespan expressed in hours. If m/s/ms are present, * they will be truncated! */ public long getDurationInHours() { return getDuration(TimeUnit.HOUR); }
/** * 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); }
/** * @return the duration of this timespan expressed in seconds. If milliseconds are present, * they will be truncated! */ public long getDurationInSeconds() { return getDuration(TimeUnit.SECOND); }
/** * 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); }
/** * @return the duration of this timespan expressed in minutes. If s/ms are present, * they will be truncated! */ public long getDurationInMinutes() { return getDuration(TimeUnit.MINUTE); }
/** * 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); }
/** * @return the duration of this timespan expressed in seconds. If milliseconds are present, * they will be truncated! */ public long getDurationInSeconds() { return getDuration(TimeUnit.SECOND); }
/** * 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); }
/** * @return the duration of this timespan expressed in minutes. If s/ms are present, * they will be truncated! */ public long getDurationInMinutes() { return getDuration(TimeUnit.MINUTE); }
/** * @param timeUnit * the unit of time you want this timespan ass * * @return the duration of this timespan expressed in the time unit provided. Note that all * units below timeUnit will be truncated! (ex: 3h20m45s will return 3 if timeUnit=HOUR). */ public long getDuration(TimeUnit timeUnit) { return truncate(timeUnit).getDuration(); }
/** * @param timeUnit * the unit of time you want this timespan ass * * @return the duration of this timespan expressed in the time unit provided. Note that all * units below timeUnit will be truncated! (ex: 3h20m45s will return 3 if timeUnit=HOUR). */ public long getDuration(TimeUnit timeUnit) { return truncate(timeUnit).getDuration(); }
/** * @return the duration of this timespan in milliseconds */ public long getDurationInMilliseconds() { // 100% equivalent to getDuration(TimeUnit.MILLISECOND) but faster! return getDuration() * getTimeUnit().getMillisecondsCount(); }
/** * @return the duration of this timespan in milliseconds */ public long getDurationInMilliseconds() { // 100% equivalent to getDuration(TimeUnit.MILLISECOND) but faster! return getDuration() * getTimeUnit().getMillisecondsCount(); }
if (timeUnits.contains(timeUnit)) { Timespan timespan = canonicalTimespans.get(timeUnit); if (timespan != null && timespan.getDuration() > 0) { if (res == null) { res = timespan;
/** * Returns a string representing this timespan expressed with the units provided. * * @param timeUnits * the timeunits you want in the decomposition * * @return a string representation using the units. * * @see #getAsTimespans(EnumSet) */ public String getAsString(EnumSet<TimeUnit> timeUnits) { StringBuilder sb = new StringBuilder(); EnumMap<TimeUnit, Timespan> canonicalTimespans = getAsTimespans(timeUnits); for (TimeUnit timeUnit : TIME_UNIT_ORDER) { if (canonicalTimespans.containsKey(timeUnit)) { long duration = canonicalTimespans.get(timeUnit).getDuration(); if (duration > 0) { sb.append(duration).append(timeUnit.getDisplayChar()); } } } if (sb.length() == 0) { sb.append(0); if (timeUnits.contains(getTimeUnit())) { sb.append(getTimeUnit().getDisplayChar()); } } return sb.toString(); }
/** * Returns a string representing this timespan expressed with the units provided. * * @param timeUnits * the timeunits you want in the decomposition * * @return a string representation using the units. * * @see #getAsTimespans(EnumSet) */ public String getAsString(EnumSet<TimeUnit> timeUnits) { StringBuilder sb = new StringBuilder(); EnumMap<TimeUnit, Timespan> canonicalTimespans = getAsTimespans(timeUnits); for (TimeUnit timeUnit : TIME_UNIT_ORDER) { if (canonicalTimespans.containsKey(timeUnit)) { long duration = canonicalTimespans.get(timeUnit).getDuration(); if (duration > 0) { sb.append(duration).append(timeUnit.getDisplayChar()); } } } if (sb.length() == 0) { sb.append(0); if (timeUnits.contains(getTimeUnit())) { sb.append(getTimeUnit().getDisplayChar()); } } return sb.toString(); }
if (timeUnits.contains(timeUnit)) { Timespan timespan = canonicalTimespans.get(timeUnit); if (timespan != null && timespan.getDuration() > 0) { if (res == null) { res = timespan;