Format time : Time « Development Class « Java






Format time

     

/*
 * @(#)TimeUtil.java  1.0 Mar 7, 2008
 *
 *  The MIT License
 *
 *  Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
//package org.eoti.util;

import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TimeUtil {
  public static Unit Nanosecond = Unit.Nanosecond;
  public static Unit Microsecond = Unit.Microsecond;
  public static Unit Millisecond = Unit.Millisecond;
  public static Unit Centisecond = Unit.Centisecond;
  public static Unit Second = Unit.Second;
  public static Unit Minute = Unit.Minute;
  public static Unit Hour = Unit.Hour;
  public static Unit Day = Unit.Day;

  public enum Unit {
    Nanosecond("ns", NANOSECONDS, 1), Microsecond('\u00B5' + "s",
        MICROSECONDS, 1), Millisecond("ms", MILLISECONDS, 1), Centisecond(
        "cs", MILLISECONDS, 10), Second("sec", SECONDS, 1), Minute(
        "min", SECONDS, 60), Hour("hr", SECONDS, 60 * 60), Day("day",
        SECONDS, 60 * 60 * 24);

    Unit(String friendlyName, TimeUnit unit, long multiplier) {
      this.friendlyName = friendlyName;
      this.unit = unit;
      this.multiplier = multiplier;
    }

    protected String friendlyName;
    protected TimeUnit unit;
    protected long multiplier;

    public long convertFrom(long amount, Unit unitOfAmount) {
      if (unit.equals(unitOfAmount.unit))
        return amount * unitOfAmount.multiplier / multiplier;

      return unit.convert(amount * unitOfAmount.multiplier,
          unitOfAmount.unit) / multiplier;
    }

    public void sleep(long timeout) {
      try {
        unit.sleep(timeout * multiplier);
      } catch (InterruptedException ie) {
        // no-op
      }
    }

    public void timedJoin(Thread thread, long timeout) {
      try {
        unit.timedJoin(thread, timeout * multiplier);
      } catch (InterruptedException ie) {
        // no-op
      }
    }

    public void timedWait(Object obj, long timeout) {
      try {
        unit.timedWait(obj, timeout * multiplier);
      } catch (InterruptedException ie) {
        // no-op
      }
    }

    public String toString(long amount) {
      return String.format("%d %ss", amount, toString());
    }

    public String friendly(long amount) {
      return String.format("%d%s", amount, friendlyName);
    }
  }

  public static String friendly(long amount, Unit unitOfAmount,
      boolean includeCentiseconds) {
    long nanos = Nanosecond.convertFrom(amount, unitOfAmount);
    List<Unit> units = Arrays.asList(Unit.values());
    Collections.reverse(units);
    StringBuilder sb = new StringBuilder();
    for (Unit unit : units) {
      if ((!includeCentiseconds) && unit.equals(Centisecond))
        continue;

      long unitAmount = unit.convertFrom(nanos, Nanosecond);
      nanos -= Nanosecond.convertFrom(unitAmount, unit);
      if (unitAmount > 0)
        sb.append(String.format("%s ", unit.friendly(unitAmount)));
    }

    return sb.toString().trim();
  }

  public static String unfriendly(long amount, Unit unitOfAmount,
      boolean includeCentiseconds) {
    long nanos = Nanosecond.convertFrom(amount, unitOfAmount);
    List<Unit> units = Arrays.asList(Unit.values());
    Collections.reverse(units);
    StringBuilder sb = new StringBuilder();
    for (Unit unit : units) {
      if ((!includeCentiseconds) && unit.equals(Centisecond))
        continue;

      long unitAmount = unit.convertFrom(nanos, Nanosecond);
      nanos -= Nanosecond.convertFrom(unitAmount, unit);
      if (unitAmount > 0)
        sb.append(String.format("%s ", unit.toString(unitAmount)));
    }

    return sb.toString().trim();
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Get Time From Date
2.ISO8601 Date Time Format
3.Time Format
4.Time Formatter
5.Returns time string
6.Returns the given date with the time values cleared
7.Convert the time to the midnight of the currently set date
8.Compare both times and dates
9.Tells you if the date part of a datetime is in a certain time range
10.Returns the given date with time set to the end of the day
11.Convert milliseconds to readable string
12.Determines whether or not a date has any time values (hour, minute, seconds or millisecondsReturns the given date with the time values cleared
13.Returns a formatted String from time
14.Time library
15.Elapsed time in hours/minutes/seconds
16.Sets the time on the same day to 00:00:00.000
17.Determines if given times span at least an entire day
18.Converts a given time in milliseconds into a XMLGregorianCalendar object.
19.Time Distance
20.Time Formatter
21.A utility class for representing a span of time.
22.GmtCalendar is a useful class for working with times that are based using GMT time.
23.Time Period
24.Represents the TSTInfo strcture within a time-stamp token (RFC 3161).
25.SimpleTimer enables bounded and unbounded waits.
26.Takes a time in milliseconds and returns an hours, minutes and seconds representation.
27.Format time in milliseconds