Added method for converting millis interval into human readable format.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@887 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-11-05 02:24:22 +00:00
parent 6c5df91f83
commit 81adae5fd3
@@ -1,5 +1,5 @@
//
// $Id: StringUtil.java,v 1.41 2002/10/31 01:44:25 shaper Exp $
// $Id: StringUtil.java,v 1.42 2002/11/05 02:24:22 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -814,5 +814,40 @@ public class StringUtil
return buf.toString();
}
/**
* Used to convert a time interval to a more easily human readable
* string of the form: <code>1d 15h 4m 15s 987m</code>.
*/
public static String intervalToString (long millis)
{
StringBuffer buf = new StringBuffer();
boolean started = false;
long days = millis / (24 * 60 * 60 * 1000);
if (days != 0) {
buf.append(days).append("d ");
started = true;
}
long hours = (millis / (60 * 60 * 1000)) % 24;
if (started || hours != 0) {
buf.append(hours).append("h ");
}
long minutes = (millis / (60 * 1000)) % 60;
if (started || minutes != 0) {
buf.append(minutes).append("m ");
}
long seconds = (millis / (1000)) % 60;
if (started || seconds != 0) {
buf.append(seconds).append("s ");
}
buf.append(millis % 1000).append("m");
return buf.toString();
}
private final static String XLATE = "0123456789abcdef";
}