[Schmitzm-commits] r1771 - in trunk/schmitzm-core/src: main/java/de/schmitzm/lang test/java/de/schmitzm/lang

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 4 10:28:11 CET 2011


Author: alfonx
Date: 2011-11-04 10:28:09 +0100 (Fri, 04 Nov 2011)
New Revision: 1771

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
Log:

	/**
	 * Formatiert eine Zeitspanne als Text 
	 */
	public static String formatInterval(final long l) {

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-11-03 21:06:37 UTC (rev 1770)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-11-04 09:28:09 UTC (rev 1771)
@@ -51,6 +51,7 @@
 import java.util.Locale;
 import java.util.TreeSet;
 import java.util.Vector;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -2074,4 +2075,21 @@
 		return new Date(cal.getTimeInMillis());
 	}
 
+	/**
+	 * Formatiert eine Zeitspanne als Text 
+	 */
+	public static String formatInterval(final long l) {
+		final long days = TimeUnit.MILLISECONDS.toDays(l);
+		final long hr = TimeUnit.MILLISECONDS.toHours(l);
+		final long min = TimeUnit.MILLISECONDS.toMinutes(l - TimeUnit.HOURS.toMillis(hr));
+		final long sec = TimeUnit.MILLISECONDS.toSeconds(l - TimeUnit.HOURS.toMillis(hr)
+				- TimeUnit.MINUTES.toMillis(min));
+		final long ms = TimeUnit.MILLISECONDS.toMillis(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min)
+				- TimeUnit.SECONDS.toMillis(sec));
+		if (days > 0)
+			return String.format("%ddays %02d:%02d:%02d.%03d", days, hr, min, sec, ms);
+		else
+			return String.format("%02d:%02d:%02d.%03d", days, hr, min, sec, ms);
+	}
+
 }

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java	2011-11-03 21:06:37 UTC (rev 1770)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java	2011-11-04 09:28:09 UTC (rev 1771)
@@ -5,6 +5,7 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.lang.management.ManagementFactory;
 import java.util.Enumeration;
 
 import org.apache.log4j.Level;
@@ -172,4 +173,10 @@
 	  assertTrue(ret[1] == null); // String.class nicht von LangUtilTest abgeleitet
       assertEquals(value, ret[2]);
 	}
+
+	@Test
+	public void testFormatInterval()
+	{
+		System.out.println( LangUtil.formatInterval(ManagementFactory.getRuntimeMXBean().getUptime()) );
+	}
 }
\ No newline at end of file



More information about the Schmitzm-commits mailing list