--- projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java 2001/02/03 23:03:30 1.1 +++ projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java 2001/02/05 22:21:20 1.2 @@ -9,7 +9,7 @@ import java.text.*; * Provides easy to use date functions. * * @author $Author: tdb $ - * @version $Id: DateUtils.java,v 1.1 2001/02/03 23:03:30 tdb Exp $ + * @version $Id: DateUtils.java,v 1.2 2001/02/05 22:21:20 tdb Exp $ */ public class DateUtils { @@ -18,13 +18,19 @@ public class DateUtils { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.1 $"; + public final String REVISION = "$Revision: 1.2 $"; public static final long secsPerMonth = 30*24*60*60; public static final long secsPerWeek = 7*24*60*60; public static final long secsPerDay = 24*60*60; public static final long secsPerHour = 60*60; + public static final long secsPerMin = 60; + private static final String daysKey = "%DAYS%"; + private static final String hoursKey = "%HOURS%"; + private static final String minsKey = "%MINS%"; + private static final String secsKey = "%SECS%"; + //---STATIC METHODS--- /** @@ -119,8 +125,8 @@ public class DateUtils { SimpleDateFormat formatter = new SimpleDateFormat("EEE"); Date conv = new Date(t*1000); return formatter.format(conv); - } - + } + /** * Return a String representation of the time (short format). */ @@ -128,8 +134,63 @@ public class DateUtils { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); Date conv = new Date(t*1000); return formatter.format(conv); - } + } + /** + * Format a long time (in seconds) as a String. Can be used to + * clearly layout a period of time in days, hours, minutes and + * seconds. It could, for example, be used to format an uptime. + * This method uses a built in default layout, as shown here; + * "Days: xx, Hours: xx, Mins: xx, Secs: xx" + * + * @param time A long value representing the time period in seconds + * @return A string representation of the given time period + */ + public static String formatTime(long time) { + String defaultLayout = "Days: "+daysKey+", Hours: "+hoursKey+", Mins: "+minsKey+", Secs: "+secsKey; + return formatTime(time, defaultLayout); + } + + /** + * Format a long time (in seconds) as a String. Can be used to + * clearly layout a period of time in days, hours, minutes and + * seconds. It could, for example, be used to format an uptime. + * This method uses a custom layout given as a String. This string + * should contain the following keys; + * "%DAYS% %HOURS% %MINS% %SECS%" + * As an example, the default layout given by formatTime() is + * represented by the following layout String; + * "Days: %DAYS%, Hours: %HOURS%, Mins: %MINS%, Secs: %SECS%" + * + * @param time A long value representing the time period in seconds + * @param layout The custom layout format to use. + * @return A string representation of the given time period + */ + public static String formatTime(long time, String layout) { + // calculate days + String days = new Long(time / secsPerDay).toString(); + time = time % secsPerDay; + + // calculate hours + String hours = new Long(time / secsPerHour).toString(); + time = time % secsPerHour; + + // calculate minutes + String mins = new Long(time / secsPerMin).toString(); + time = time % secsPerMin; + + // seconds are left over + String secs = new Long(time).toString(); + + // put the values into the layout + layout = StringUtils.replaceText(layout, daysKey, days); + layout = StringUtils.replaceText(layout, hoursKey, hours); + layout = StringUtils.replaceText(layout, minsKey, mins); + layout = StringUtils.replaceText(layout, secsKey, secs); + + return layout; + } + //---CONSTRUCTORS--- @@ -150,7 +211,7 @@ public class DateUtils { getClass().getName(), REVISION); } - + //---PRIVATE METHODS--- //---ACCESSOR/MUTATOR METHODS---