KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > DateUtil


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.util;
38
39 import java.text.DateFormat JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.TimeZone JavaDoc;
45
46 import net.sourceforge.cruisecontrol.CruiseControlException;
47
48 import org.apache.log4j.Logger;
49
50 public final class DateUtil {
51
52     private static final Logger LOG = Logger.getLogger(DateUtil.class);
53
54     public static final transient long ONE_SECOND = 1000;
55
56     public static final long ONE_MINUTE = 60 * ONE_SECOND;
57
58     static final long ONE_HOUR = 60 * ONE_MINUTE;
59
60     public static final String JavaDoc SIMPLE_DATE_FORMAT = "yyyyMMddHHmmss";
61
62     /**
63      * This is the date format required by commands passed to CVS.
64      */

65     // The timezone is hard coded to GMT to prevent problems with it being
66
// formatted as GMT+00:00. However, we still need to set the time zone
67
// of the formatter so that it knows it's in GMT.
68
private static final String JavaDoc CVS_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss 'GMT'";
69
70     // This cannot be exposed as TimeZones are mutable
71
private static final TimeZone JavaDoc UTC = TimeZone.getTimeZone("Etc/UTC");
72
73     private DateUtil() {
74     }
75
76     /**
77      * Create an integer time from a <code>Date</code> object.
78      *
79      * @param date
80      * The date to get the timestamp from.
81      * @return The time as an integer formatted as "HHmm".
82      */

83     public static int getTimeFromDate(Date JavaDoc date) {
84         Calendar JavaDoc calendar = Calendar.getInstance();
85         calendar.setTime(date);
86         int hour = calendar.get(Calendar.HOUR_OF_DAY) * 100;
87         int minute = calendar.get(Calendar.MINUTE);
88         return hour + minute;
89     }
90
91     /**
92      * @deprecated Use correctly spelled <code>milliTimeDifference</code> instead.
93      */

94     public static long milliTimeDiffernce(int earlier, int later) {
95         return milliTimeDifference(earlier, later);
96     }
97
98     /**
99      * finds the difference in milliseconds between two integer time values of the format "HHmm".
100      *
101      * @param earlier
102      * integer time value of format "HHmm"
103      * @param later
104      * integer time value of format "HHmm"
105      * @return long millisecond time difference
106      */

107     public static long milliTimeDifference(int earlier, int later) {
108         long earlierMillis = convertToMillis(earlier);
109         long laterMillis = convertToMillis(later);
110         return laterMillis - earlierMillis;
111     }
112
113     /**
114      * Convert a time represented by the format "HHmm" into milliseconds.
115      *
116      * @param hhmm
117      * where hh are hours and mm are minutes
118      * @return hhmm in milliseconds
119      */

120     public static long convertToMillis(int hhmm) {
121         int minutes = hhmm % 100;
122         int hours = (hhmm - minutes) / 100;
123         return hours * ONE_HOUR + minutes * ONE_MINUTE;
124     }
125
126     /**
127      * @param time
128      * time in milliseconds
129      * @return Time formatted as X hours Y minutes Z seconds
130      */

131     public static String JavaDoc formatTime(long time) {
132         long seconds = time / 1000;
133         long hours = seconds / 3600;
134         long minutes = (seconds % 3600) / 60;
135         seconds = seconds % 60;
136
137         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
138         if (hours != 0) {
139             sb.append(hours).append(" hours ");
140         }
141         if (minutes != 0) {
142             sb.append(minutes).append(" minutes ");
143         }
144         if (seconds != 0) {
145             sb.append(seconds).append(" seconds");
146         }
147
148         return sb.toString();
149     }
150
151     /**
152      * @return midnight on today's date
153      */

154     public static Date JavaDoc getMidnight() {
155         Calendar JavaDoc midnight = Calendar.getInstance();
156         midnight.set(Calendar.HOUR_OF_DAY, 0);
157         midnight.set(Calendar.MINUTE, 0);
158         midnight.set(Calendar.SECOND, 0);
159         midnight.set(Calendar.MILLISECOND, 0);
160         return midnight.getTime();
161     }
162
163     public static String JavaDoc getFormattedTime(Date JavaDoc date) {
164         if (date == null) {
165             return null;
166         }
167         return new SimpleDateFormat JavaDoc(SIMPLE_DATE_FORMAT).format(date);
168     }
169
170     public static Date JavaDoc parseFormattedTime(String JavaDoc timeString, String JavaDoc description) throws CruiseControlException {
171
172         Date JavaDoc date;
173         if (timeString == null) {
174             throw new IllegalArgumentException JavaDoc("Null date string for " + description);
175         }
176         try {
177             date = new SimpleDateFormat JavaDoc(SIMPLE_DATE_FORMAT).parse(timeString);
178         } catch (ParseException JavaDoc e) {
179             LOG.error("Error parsing timestamp for [" + description + "]", e);
180             throw new CruiseControlException("Cannot parse string for " + description + ":" + timeString);
181         }
182
183         return date;
184     }
185
186     /**
187      * Formats a given date as a the format required by commands passed to CVS.
188      * @param date the date
189      */

190     public static String JavaDoc formatCVSDate(Date JavaDoc date) {
191         DateFormat JavaDoc format = new SimpleDateFormat JavaDoc(CVS_DATE_PATTERN);
192         format.setTimeZone(UTC);
193         return format.format(date);
194     }
195
196     /**
197      * Parses a text in CVS date format as a date.
198      * @param text the date to parse
199      * @return a date in the default timezone
200      */

201     public static Date JavaDoc parseCVSDate(String JavaDoc text) throws ParseException JavaDoc {
202         DateFormat JavaDoc format = new SimpleDateFormat JavaDoc(CVS_DATE_PATTERN);
203         format.setTimeZone(UTC);
204         return format.parse(text);
205     }
206
207     /**
208      * Return a String representation of a duration specified in milliseconds.
209      */

210     public static String JavaDoc getDurationAsString(final long buildLength) {
211         long timeSeconds = buildLength / 1000;
212         long minutes = (timeSeconds / 60);
213         long seconds = timeSeconds - (minutes * 60);
214         return minutes + " minute(s) " + seconds + " second(s)";
215     }
216 }
217
Popular Tags