net.sf.jabb.util.text
Class DurationFormatter

java.lang.Object
  extended by net.sf.jabb.util.text.DurationFormatter

public class DurationFormatter
extends Object

An utility to format the length of time period to String, for example: 00:03:01.011 or 7d, 12:32:02:001.
一个可以用来格式化时间段信息的工具类,其输出结果比如: 00:03:01.011,或:7d, 12:32:02:001。

An example of usage:
典型用法是这样的:

   long t0 = System.currentTimeMillis();
   ...
   long t1 = System.currentTimeMillis();
   ...
   System.out.println(DurationFormatter.formatSince(t0));  // from t0 to current time
   ...
   System.out.println(DurationFormatter.format(t1-t0)); // from t0 to t1
   ...
   Calendar calendar = new GregorianCalendar();
   calendar.set(2060, 1, 31, 8, 0);  // Jan 31 2060 8AM
   System.out.println(DurationFormatter.formatSince(calendar.getTimeInMillis()));
   
 

Author:
Zhengmao HU (James)

Field Summary
protected static long ONE_DAY_IN_MILLI
           
 
Constructor Summary
DurationFormatter()
           
 
Method Summary
static String format(long duration)
          Format the length of time period (in milliseconds) to String.
格式化时间长度(以毫秒为单位)为字符串。
static String format(long duration, TimeUnit unit, boolean trimLeftZeros)
          Format the length of time period (in milliseconds) to String.
格式化时间长度(以毫秒为单位)为字符串。
static String formatSince(long pastOrFutureTime)
          Format the length of time period between current system time and specified time to String.
格式化指定时间同当前系统时间之间的时间差为字符串。
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONE_DAY_IN_MILLI

protected static final long ONE_DAY_IN_MILLI
See Also:
Constant Field Values
Constructor Detail

DurationFormatter

public DurationFormatter()
Method Detail

format

public static String format(long duration,
                            TimeUnit unit,
                            boolean trimLeftZeros)
Format the length of time period (in milliseconds) to String.
格式化时间长度(以毫秒为单位)为字符串。

Parameters:
duration - Length of time period in milliseconds
以毫秒为单位的时间长度
unit - The least unit that will be in the output
输出字符串中的最小时间单位
trimLeftZeros - Whether to remove leading zeros from the output
输出字符串中是否去掉开头的0
Returns:
The string presentation of the time period, for example: 00:03:01.011, 1:30:00, or 7d, 12:32:02:001.
字符串形式表示的时间长度,比如:00:03:01.011, 1:30:00, 或:7d, 12:32:02:001。

format

public static String format(long duration)
Format the length of time period (in milliseconds) to String.
格式化时间长度(以毫秒为单位)为字符串。

Parameters:
duration - Length of time period in milliseconds
以毫秒为单位的时间长度
unit - The least unit that will be in the output
输出字符串中的最小时间单位
Returns:
The string presentation of the time period, for example: 00:03:01.011 or 7d, 12:32:02:001.
字符串形式表示的时间长度,比如:00:03:01.011,或:7d, 12:32:02:001。

formatSince

public static String formatSince(long pastOrFutureTime)
Format the length of time period between current system time and specified time to String.
格式化指定时间同当前系统时间之间的时间差为字符串。

The time specified can be in the past or in the future, in either cases, meaningful result will be get.

指定的用作同当前系统时间作比较的时间可以是在过去,也可以是在将来,这两种情况都能产生有意义的结果。

Parameters:
pastOrFutureTime - A time in the past or in the future
过去或未来的某个时间
Returns:
The string presentation of the time period, for example: 00:03:01.011 or 7d, 12:32:02:001.
字符串形式表示的时间长度,比如:00:03:01.011,或:7d, 12:32:02:001。


Copyright © 2012. All Rights Reserved.