From 81adae5fd34eb3cc7c35c362717663dd7565ac3c Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 5 Nov 2002 02:24:22 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/StringUtil.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 8cfd252d..ce986e67 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -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: 1d 15h 4m 15s 987m. + */ + 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"; }