ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java (file contents):
Revision 1.1 by tdb, Sat Feb 3 23:03:30 2001 UTC vs.
Revision 1.2 by tdb, Mon Feb 5 22:21:20 2001 UTC

# Line 24 | Line 24 | public class DateUtils {
24      public static final long secsPerWeek = 7*24*60*60;
25      public static final long secsPerDay = 24*60*60;
26      public static final long secsPerHour = 60*60;
27 +    public static final long secsPerMin = 60;
28      
29 +    private static final String daysKey = "%DAYS%";
30 +    private static final String hoursKey = "%HOURS%";
31 +    private static final String minsKey = "%MINS%";
32 +    private static final String secsKey = "%SECS%";
33 +    
34   //---STATIC METHODS---
35  
36      /**
# Line 119 | Line 125 | public class DateUtils {
125          SimpleDateFormat formatter = new SimpleDateFormat("EEE");
126          Date conv = new Date(t*1000);
127          return formatter.format(conv);
128 <    }    
129 <
128 >    }
129 >    
130      /**
131       * Return a String representation of the time (short format).
132       */
# Line 128 | Line 134 | public class DateUtils {
134          SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
135          Date conv = new Date(t*1000);
136          return formatter.format(conv);
137 <    }    
137 >    }
138      
139 +    /**
140 +     * Format a long time (in seconds) as a String. Can be used to
141 +     * clearly layout a period of time in days, hours, minutes and
142 +     * seconds. It could, for example, be used to format an uptime.
143 +     * This method uses a built in default layout, as shown here;
144 +     *   "Days: xx,  Hours: xx,  Mins: xx,  Secs: xx"
145 +     *
146 +     * @param time A long value representing the time period in seconds
147 +     * @return A string representation of the given time period
148 +     */
149 +    public static String formatTime(long time) {
150 +        String defaultLayout = "Days: "+daysKey+",  Hours: "+hoursKey+",  Mins: "+minsKey+",  Secs: "+secsKey;
151 +        return formatTime(time, defaultLayout);
152 +    }
153 +    
154 +    /**
155 +     * Format a long time (in seconds) as a String. Can be used to
156 +     * clearly layout a period of time in days, hours, minutes and
157 +     * seconds. It could, for example, be used to format an uptime.
158 +     * This method uses a custom layout given as a String. This string
159 +     * should contain the following keys;
160 +     *   "%DAYS% %HOURS% %MINS% %SECS%"
161 +     * As an example, the default layout given by formatTime() is
162 +     * represented by the following layout String;
163 +     *   "Days: %DAYS%,  Hours: %HOURS%,  Mins: %MINS%,  Secs: %SECS%"
164 +     *
165 +     * @param time A long value representing the time period in seconds
166 +     * @param layout The custom layout format to use.
167 +     * @return A string representation of the given time period
168 +     */
169 +    public static String formatTime(long time, String layout) {
170 +        // calculate days
171 +        String days = new Long(time / secsPerDay).toString();
172 +        time = time % secsPerDay;
173 +        
174 +        // calculate hours
175 +        String hours = new Long(time / secsPerHour).toString();
176 +        time = time % secsPerHour;
177 +        
178 +        // calculate minutes
179 +        String mins = new Long(time / secsPerMin).toString();
180 +        time = time % secsPerMin;
181 +        
182 +        // seconds are left over
183 +        String secs = new Long(time).toString();
184 +        
185 +        // put the values into the layout
186 +        layout = StringUtils.replaceText(layout, daysKey, days);
187 +        layout = StringUtils.replaceText(layout, hoursKey, hours);
188 +        layout = StringUtils.replaceText(layout, minsKey, mins);
189 +        layout = StringUtils.replaceText(layout, secsKey, secs);
190 +        
191 +        return layout;
192 +    }
193 +    
194  
195   //---CONSTRUCTORS---
196  
# Line 150 | Line 211 | public class DateUtils {
211              getClass().getName(),
212              REVISION);
213      }
214 <
214 >    
215   //---PRIVATE METHODS---
216  
217   //---ACCESSOR/MUTATOR METHODS---

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines