Java Code Examples for java.util.concurrent.TimeUnit#compareTo()

The following examples show how to use java.util.concurrent.TimeUnit#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Time.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static long roundFromMillis(long millis, TimeUnit units) {
    if (units.compareTo(TimeUnit.MILLISECONDS) > 0) {
        double result = ((double)millis) / units.toMillis(1);
        return Math.round(result);
    } else {
        return units.convert(millis, TimeUnit.MILLISECONDS);
    }
}
 
Example 2
Source File: PrettyTime.java    From Time4A with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Formats given time point relative to the current time of {@link #getReferenceClock()}
 * as duration in given precision or less. </p>
 *
 * <p>If day precision is given then output like &quot;today&quot;, &quot;yesterday&quot;,
 * &quot;tomorrow&quot; or &quot;last Wednesday&quot; etc. is possible. Example: </p>
 *
 * <pre>
 *      TimeSource&lt;?&gt; clock = new TimeSource&lt;Moment&gt;() {
 *          public Moment currentTime() {
 *              return PlainTimestamp.of(2015, 8, 1, 10, 24, 5).atUTC();
 *          }};
 *      String durationInDays =
 *          PrettyTime.of(Locale.GERMAN).withReferenceClock(clock).printRelative(
 *              PlainTimestamp.of(2015, 8, 1, 17, 0).atUTC(),
 *              Timezone.of(EUROPE.BERLIN),
 *              TimeUnit.DAYS);
 *      System.out.println(durationInDays); // heute (german word for today)
 * </pre>
 *
 * @param   moment      relative time point
 * @param   tz          time zone for translating to a local duration
 * @param   precision   maximum precision of relative time (not more than seconds)
 * @return  formatted output of relative time, either in past or in future
 * @since   3.6/4.4
 */
/*[deutsch]
 * <p>Formatiert den angegebenen Zeitpunkt relativ zur aktuellen Zeit
 * der Referenzuhr {@link #getReferenceClock()} als Dauer in der angegebenen
 * maximalen Genauigkeit. </p>
 *
 * <p>Wenn Tagesgenauigkeit angegeben ist, sind auch Ausgaben wie &quot;heute&quot;, &quot;gestern&quot;,
 * &quot;morgen&quot; oder &quot;letzten Mittwoch&quot; etc. m&ouml;glich. Beispiel: </p>
 *
 * <pre>
 *      TimeSource&lt;?&gt; clock = new TimeSource&lt;Moment&gt;() {
 *          public Moment currentTime() {
 *              return PlainTimestamp.of(2015, 8, 1, 10, 24, 5).atUTC();
 *          }};
 *      String durationInDays =
 *          PrettyTime.of(Locale.GERMAN).withReferenceClock(clock).printRelative(
 *              PlainTimestamp.of(2015, 8, 1, 17, 0).atUTC(),
 *              Timezone.of(EUROPE.BERLIN),
 *              TimeUnit.DAYS);
 *      System.out.println(durationInDays); // heute
 * </pre>
 *
 * @param   moment      relative time point
 * @param   tz          time zone for translating to a local duration
 * @param   precision   maximum precision of relative time (not more than seconds)
 * @return  formatted output of relative time, either in past or in future
 * @since   3.6/4.4
 */
public String printRelative(
    UnixTime moment,
    Timezone tz,
    TimeUnit precision
) {

    UnixTime ref = this.getReferenceClock().currentTime();
    Moment t1 = Moment.from(ref);
    Moment t2 = Moment.from(moment);

    if (precision.compareTo(TimeUnit.SECONDS) <= 0) {
        long delta = t1.until(t2, TimeUnit.SECONDS);

        if (Math.abs(delta) < 60L) {
            return this.printRelativeSeconds(t1, t2, delta);
        }
    }

    return this.printRelativeTime(t1, t2, tz, precision, null, null);

}
 
Example 3
Source File: PrettyTime.java    From Time4A with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Formats given time point relative to the current time of {@link #getReferenceClock()}
 * as duration in given precision or as absolute date-time. </p>
 *
 * <p>If the calculated duration in seconds is bigger than {@code maxdelta} then the absolute date-time
 * will be printed else a relative expression will be used. </p>
 *
 * @param   moment      relative time point
 * @param   tz          time zone for translating to a local duration
 * @param   precision   maximum precision of relative time (not more than seconds)
 * @param   maxdelta    maximum deviation of given moment from clock in posix seconds for relative printing
 * @param   formatter   used for printing absolute time if the deviation is bigger than maxdelta
 * @return  formatted output of relative time, either in past or in future
 * @since   3.6/4.4
 */
/*[deutsch]
 * <p>Formatiert den angegebenen Zeitpunkt relativ zur aktuellen Zeit
 * der Referenzuhr {@link #getReferenceClock()} als Dauer in der angegebenen
 * maximalen Genauigkeit oder als absolute Datumszeit. </p>
 *
 * <p>Wenn die berechnete Dauer in Sekunden gr&ouml;&szlig;er als {@code maxdelta} ist, wird
 * die absolute Datumszeit formatiert, sonst wird ein relativer Ausdruck verwendet. </p>
 *
 * @param   moment      relative time point
 * @param   tz          time zone for translating to a local duration
 * @param   precision   maximum precision of relative time (not more than seconds)
 * @param   maxdelta    maximum deviation of given moment from clock in posix seconds for relative printing
 * @param   formatter   used for printing absolute time if the deviation is bigger than maxdelta
 * @return  formatted output of relative time, either in past or in future
 * @since   3.6/4.4
 */
public String printRelativeOrDateTime(
    UnixTime moment,
    Timezone tz,
    TimeUnit precision,
    long maxdelta,
    TemporalFormatter<Moment> formatter
) {

    UnixTime ref = this.getReferenceClock().currentTime();
    Moment t1 = Moment.from(ref);
    Moment t2 = Moment.from(moment);
    long delta = t1.until(t2, TimeUnit.SECONDS);

    if (Math.abs(delta) > maxdelta) {
        return formatter.format(t2);
    } else if (
        (precision.compareTo(TimeUnit.SECONDS) <= 0)
        && (Math.abs(delta) < 60L)
    ) {
        return this.printRelativeSeconds(t1, t2, delta);
    }

    return this.printRelativeTime(t1, t2, tz, precision, null, null);

}
 
Example 4
Source File: PrettyTime.java    From Time4A with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Formats given time point relative to the current time of {@link #getReferenceClock()}
 * as duration in given precision or as absolute date-time. </p>
 *
 * @param   moment          time point whose deviation from clock is to be printed
 * @param   tz              time zone for translating to a local duration
 * @param   precision       maximum precision of relative time (not more than seconds)
 * @param   maxRelativeUnit maximum time unit which will still be printed in a relative way
 * @param   formatter       used for printing absolute time if the leading unit is bigger than maxRelativeUnit
 * @return  formatted output of relative time, either in past or in future
 * @since   3.6/4.4
 */
/*[deutsch]
 * <p>Formatiert den angegebenen Zeitpunkt relativ zur aktuellen Zeit
 * der Referenzuhr {@link #getReferenceClock()} als Dauer in der angegebenen
 * maximalen Genauigkeit oder als absolute Datumszeit. </p>
 *
 * @param   moment          time point whose deviation from clock is to be printed
 * @param   tz              time zone for translating to a local duration
 * @param   precision       maximum precision of relative time (not more than seconds)
 * @param   maxRelativeUnit maximum time unit which will still be printed in a relative way
 * @param   formatter       used for printing absolute time if the leading unit is bigger than maxRelativeUnit
 * @return  formatted output of relative time, either in past or in future
 * @since   3.6/4.4
 */
public String printRelativeOrDateTime(
    UnixTime moment,
    Timezone tz,
    TimeUnit precision,
    CalendarUnit maxRelativeUnit,
    TemporalFormatter<Moment> formatter
) {

    if (maxRelativeUnit == null) {
        throw new NullPointerException("Missing max relative unit.");
    }

    UnixTime ref = this.getReferenceClock().currentTime();
    Moment t1 = Moment.from(ref);
    Moment t2 = Moment.from(moment);
    long delta = t1.until(t2, TimeUnit.SECONDS);

    if (
        (precision.compareTo(TimeUnit.SECONDS) <= 0)
        && (Math.abs(delta) < 60L)
    ) {
        return this.printRelativeSeconds(t1, t2, delta);
    }

    return this.printRelativeTime(t1, t2, tz, precision, maxRelativeUnit, formatter);

}
 
Example 5
Source File: Scheduler.java    From reactor-core with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the "current time" notion of this scheduler.
 *
 * <p>
 *     <strong>Implementation Note:</strong> The default implementation uses {@link System#currentTimeMillis()}
 *     when requested with a {@code TimeUnit} of {@link TimeUnit#MILLISECONDS milliseconds} or coarser, and
 *     {@link System#nanoTime()} otherwise. As a consequence, results should not be interpreted as absolute timestamps
 *     in the latter case, only monotonicity inside the current JVM can be expected.
 * </p>
 * @param unit the target unit of the current time
 * @return the current time value in the target unit of measure
 */
default long now(TimeUnit unit) {
	if (unit.compareTo(TimeUnit.MILLISECONDS) >= 0) {
		return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
	} else {
		return unit.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
	}
}