Java tutorial
/* * Licensed under the GPL License. You may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ package net.testdriven.psiprobe.jsp; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Silly JSP tag to display duration in milliseconds as hours:minutes:seconds.milliseconds * * @author Vlad Ilyushchenko * @author Mark Lewis */ public class DurationTag extends TagSupport { private static final long serialVersionUID = 1L; private static Log logger = LogFactory.getLog(DurationTag.class); private long value; public void setValue(long value) { this.value = value; } @Override public int doStartTag() throws JspException { try { pageContext.getOut().write(duration(value)); } catch (IOException e) { logger.debug("Exception writing duration to JspWriter", e); throw new JspException(e); } return EVAL_BODY_INCLUDE; } public static String duration(long value) { long millis = value % 1000; long sec = value / 1000; long mins = sec / 60; long hours = mins / 60; sec = sec % 60; mins = mins % 60; return hours + ":" + long2Str(mins) + ":" + long2Str(sec) + "." + long3Str(millis); } private static String long2Str(long l) { return l < 10 ? "0" + l : Long.toString(l); } private static String long3Str(long l) { return l < 10 ? "00" + l : l < 100 ? "0" + l : Long.toString(l); } }