KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > TimeUtil


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.math.*;
19
20 import org.compiere.model.*;
21
22 /**
23  * Time Utilities
24  *
25  * @author Jorg Janke
26  * @version $Id: TimeUtil.java,v 1.1 2003/10/21 05:35:23 jjanke Exp $
27  */

28 public class TimeUtil
29 {
30     /**
31      * Get earliest time of a day (truncate)
32      * @param time day and time
33      * @return day with 00:00
34      */

35     static public Timestamp getDay (long time)
36     {
37         if (time == 0)
38             return null;
39         GregorianCalendar cal = new GregorianCalendar(Language.getLanguage().getLocale());
40         cal.setTimeInMillis(time);
41         cal.set(Calendar.HOUR_OF_DAY, 0);
42         cal.set(Calendar.MINUTE, 0);
43         cal.set(Calendar.SECOND, 0);
44         cal.set(Calendar.MILLISECOND, 0);
45         return new Timestamp (cal.getTimeInMillis());
46     } // getDay
47

48     /**
49      * Get earliest time of a day (truncate)
50      * @param dayTime day and time
51      * @return day with 00:00
52      */

53     static public Timestamp getDay (Timestamp dayTime)
54     {
55         if (dayTime == null)
56             return null;
57         return getDay(dayTime.getTime());
58     } // getDay
59

60     /**
61      * Get earliest time of a day (truncate)
62      * @param day day
63      * @param month month
64      * @param year year
65      * @return timestamp
66      */

67     static public Timestamp getDay (int day, int month, int year)
68     {
69         GregorianCalendar cal = new GregorianCalendar (year, month, day);
70         return new Timestamp (cal.getTimeInMillis());
71     } // getDay
72

73     /**
74      * Get earliest time of next day
75      * @param day day
76      * @return next day with 00:00
77      */

78     static public Timestamp getNextDay (Timestamp day)
79     {
80         if (day == null)
81             return null;
82         GregorianCalendar cal = new GregorianCalendar(Language.getLanguage().getLocale());
83         cal.setTimeInMillis(day.getTime());
84         cal.add(Calendar.DAY_OF_YEAR, +1); // next
85
cal.set(Calendar.HOUR_OF_DAY, 0);
86         cal.set(Calendar.MINUTE, 0);
87         cal.set(Calendar.SECOND, 0);
88         cal.set(Calendar.MILLISECOND, 0);
89         return new Timestamp (cal.getTimeInMillis());
90     } // getNextDay
91

92     /**
93      * Return the day and time
94      * @param day day part
95      * @param time time part
96      * @return day + time
97      */

98     static public Timestamp getDayTime (Timestamp day, Timestamp time)
99     {
100         GregorianCalendar cal_1 = new GregorianCalendar();
101         cal_1.setTimeInMillis(day.getTime());
102         GregorianCalendar cal_2 = new GregorianCalendar();
103         cal_2.setTimeInMillis(time.getTime());
104         //
105
GregorianCalendar cal = new GregorianCalendar(Language.getLanguage().getLocale());
106         cal.set(cal_1.get(Calendar.YEAR),
107             cal_1.get(Calendar.MONTH),
108             cal_1.get(Calendar.DAY_OF_MONTH),
109             cal_2.get(Calendar.HOUR_OF_DAY),
110             cal_2.get(Calendar.MINUTE),
111             cal_2.get(Calendar.SECOND));
112         cal.set(Calendar.MILLISECOND, 0);
113         Timestamp retValue = new Timestamp(cal.getTimeInMillis());
114     // Log.trace(Log.l6_Database, "TimeUtil.getDayTime", "Day=" + day + ", Time=" + time + " => " + retValue);
115
return retValue;
116     } // getDayTime
117

118     /**
119      * Is the _1 in the Range of _2
120      * <pre>
121      * Time_1 +--x--+
122      * Time_2 +a+ +---b---+ +c+
123      * </pre>
124      * The function returns true for b and false for a/b.
125      * @param start_1 start (1)
126      * @param end_1 not included end (1)
127      * @param start_2 start (2)
128      * @param end_2 not included (2)
129      * @return true if in range
130      */

131     static public boolean inRange (Timestamp start_1, Timestamp end_1, Timestamp start_2, Timestamp end_2)
132     {
133         // validity check
134
if (end_1.before(start_1))
135             throw new UnsupportedOperationException JavaDoc ("TimeUtil.inRange End_1=" + end_1 + " before Start_1=" + start_1);
136         if (end_2.before(start_2))
137             throw new UnsupportedOperationException JavaDoc ("TimeUtil.inRange End_2=" + end_2 + " before Start_2=" + start_2);
138         // case a
139
if (!end_2.after(start_1)) // end not including
140
{
141     // Log.trace(Log.l6_Database, "TimeUtil.InRange - No", start_1 + "->" + end_1 + " <??> " + start_2 + "->" + end_2);
142
return false;
143         }
144         // case c
145
if (!start_2.before(end_1)) // end not including
146
{
147     // Log.trace(Log.l6_Database, "TimeUtil.InRange - No", start_1 + "->" + end_1 + " <??> " + start_2 + "->" + end_2);
148
return false;
149         }
150     // Log.trace(Log.l6_Database, "TimeUtil.InRange - Yes", start_1 + "->" + end_1 + " <??> " + start_2 + "->" + end_2);
151
return true;
152     } // inRange
153

154     /**
155      * Is start..end on one of the days ?
156      * @param start start day
157      * @param end end day (not including)
158      * @param OnMonday true if OK
159      * @param OnTuesday true if OK
160      * @param OnWednesday true if OK
161      * @param OnThursday true if OK
162      * @param OnFriday true if OK
163      * @param OnSaturday true if OK
164      * @param OnSunday true if OK
165      * @return true if on one of the days
166      */

167     static public boolean inRange (Timestamp start, Timestamp end,
168         boolean OnMonday, boolean OnTuesday, boolean OnWednesday,
169         boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday)
170     {
171         // are there restrictions?
172
if (OnSaturday && OnSunday && OnMonday && OnTuesday && OnWednesday && OnThursday && OnFriday)
173             return false;
174
175         GregorianCalendar calStart = new GregorianCalendar();
176         calStart.setTimeInMillis(start.getTime());
177         int dayStart = calStart.get(Calendar.DAY_OF_WEEK);
178         //
179
GregorianCalendar calEnd = new GregorianCalendar();
180         calEnd.setTimeInMillis(end.getTime());
181         calEnd.add(Calendar.DAY_OF_YEAR, -1); // not including
182
int dayEnd = calEnd.get(Calendar.DAY_OF_WEEK);
183
184         // On same day
185
if (calStart.get(Calendar.YEAR) == calEnd.get(Calendar.YEAR)
186             && calStart.get(Calendar.MONTH) == calEnd.get(Calendar.MONTH)
187             && calStart.get(Calendar.DAY_OF_MONTH) == calEnd.get(Calendar.DAY_OF_YEAR))
188         {
189             if ((!OnSaturday && dayStart == Calendar.SATURDAY)
190                 || (!OnSunday && dayStart == Calendar.SUNDAY)
191                 || (!OnMonday && dayStart == Calendar.MONDAY)
192                 || (!OnTuesday && dayStart == Calendar.TUESDAY)
193                 || (!OnWednesday && dayStart == Calendar.WEDNESDAY)
194                 || (!OnThursday && dayStart == Calendar.THURSDAY)
195                 || (!OnFriday && dayStart == Calendar.FRIDAY))
196             {
197         // Log.trace(Log.l6_Database, "TimeUtil.InRange - SameDay - Yes", start + "->" + end + " - "
198
// + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
199
return true;
200             }
201         // Log.trace(Log.l6_Database, "TimeUtil.InRange - SameDay - No", start + "->" + end + " - "
202
// + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
203
return false;
204         }
205         //
206
// Log.trace(Log.l6_Database, "TimeUtil.inRange - WeekDay Start=" + dayStart + ", Incl.End=" + dayEnd);
207

208         // Calendar.SUNDAY=1 ... SATURDAY=7
209
BitSet days = new BitSet (8);
210         // Set covered days in BitArray
211
if (dayEnd <= dayStart)
212             dayEnd += 7;
213         for (int i = dayStart; i < dayEnd; i++)
214         {
215             int index = i;
216             if (index > 7)
217                 index -= 7;
218             days.set(index);
219     // System.out.println("Set index=" + index + " i=" + i);
220
}
221
222     // for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++)
223
// System.out.println("Result i=" + i + " - " + days.get(i));
224

225         // Compare days to availability
226
if ((!OnSaturday && days.get(Calendar.SATURDAY))
227             || (!OnSunday && days.get(Calendar.SUNDAY))
228             || (!OnMonday && days.get(Calendar.MONDAY))
229             || (!OnTuesday && days.get(Calendar.TUESDAY))
230             || (!OnWednesday && days.get(Calendar.WEDNESDAY))
231             || (!OnThursday && days.get(Calendar.THURSDAY))
232             || (!OnFriday && days.get(Calendar.FRIDAY)))
233         {
234     // Log.trace(Log.l6_Database, "MAssignment.InRange - Yes", start + "->" + end + " - "
235
// + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
236
return true;
237         }
238
239     // Log.trace(Log.l6_Database, "MAssignment.InRange - No", start + "->" + end + " - "
240
// + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
241
return false;
242     } // isRange
243

244
245     /**
246      * Is it the same day
247      * @param one day
248      * @param two compared day
249      * @return true if the same day
250      */

251     static public boolean isSameDay (Timestamp one, Timestamp two)
252     {
253         GregorianCalendar calOne = new GregorianCalendar();
254         calOne.setTimeInMillis(one.getTime());
255         GregorianCalendar calTwo = new GregorianCalendar();
256         calTwo.setTimeInMillis(two.getTime());
257         if (calOne.get(Calendar.YEAR) == calTwo.get(Calendar.YEAR)
258             && calOne.get(Calendar.MONTH) == calTwo.get(Calendar.MONTH)
259             && calOne.get(Calendar.DAY_OF_MONTH) == calTwo.get(Calendar.DAY_OF_YEAR))
260             return true;
261         return false;
262     } // isSameDay
263

264     /**
265      * Is all day
266      * @param start start date
267      * @param end end date
268      * @return true if all day (00:00-00:00 next day)
269      */

270     static public boolean isAllDay (Timestamp start, Timestamp end)
271     {
272         GregorianCalendar calStart = new GregorianCalendar();
273         calStart.setTimeInMillis(start.getTime());
274         GregorianCalendar calEnd = new GregorianCalendar();
275         calEnd.setTimeInMillis(end.getTime());
276         if (calStart.get(Calendar.HOUR_OF_DAY) == calEnd.get(Calendar.HOUR_OF_DAY)
277             && calStart.get(Calendar.MINUTE) == calEnd.get(Calendar.MINUTE)
278             && calStart.get(Calendar.SECOND) == calEnd.get(Calendar.SECOND)
279             && calStart.get(Calendar.MILLISECOND) == calEnd.get(Calendar.MILLISECOND)
280             && calStart.get(Calendar.HOUR_OF_DAY) == 0
281             && calStart.get(Calendar.MINUTE) == 0
282             && calStart.get(Calendar.SECOND) == 0
283             && calStart.get(Calendar.MILLISECOND) == 0
284             && start.before(end))
285             return true;
286         //
287
return false;
288     } // isAllDay
289

290     /**
291      * Calculate the number of days between start and end.
292      * @param start start date
293      * @param end end date
294      * @return number of days (0 = same)
295      */

296     static public int getDaysBetween (Timestamp start, Timestamp end)
297     {
298         boolean negative = false;
299         if (end.before(start))
300         {
301             negative = true;
302             Timestamp temp = start;
303             start = end;
304             end = temp;
305         }
306         //
307
GregorianCalendar cal = new GregorianCalendar();
308         cal.setTime(start);
309         cal.set(Calendar.HOUR_OF_DAY, 0);
310         cal.set(Calendar.MINUTE, 0);
311         cal.set(Calendar.SECOND, 0);
312         cal.set(Calendar.MILLISECOND, 0);
313         GregorianCalendar calEnd = new GregorianCalendar();
314         calEnd.setTime(end);
315         calEnd.set(Calendar.HOUR_OF_DAY, 0);
316         calEnd.set(Calendar.MINUTE, 0);
317         calEnd.set(Calendar.SECOND, 0);
318         calEnd.set(Calendar.MILLISECOND, 0);
319
320     // System.out.println("Start=" + start + ", End=" + end + ", dayStart=" + cal.get(Calendar.DAY_OF_YEAR) + ", dayEnd=" + calEnd.get(Calendar.DAY_OF_YEAR));
321

322         // in same year
323
if (cal.get(Calendar.YEAR) == calEnd.get(Calendar.YEAR))
324         {
325             if (negative)
326                 return (calEnd.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR)) * -1;
327             return calEnd.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR);
328         }
329
330         // not very efficient, but correct
331
int counter = 0;
332         while (calEnd.after(cal))
333         {
334             cal.add (Calendar.DAY_OF_YEAR, 1);
335             counter++;
336         }
337         if (negative)
338             return counter * -1;
339         return counter;
340     } // getDatesBetrween
341

342     /**
343      * Return Day + offset (truncates)
344      * @param day Day
345      * @param offset day offset
346      * @return Day + offset at 00:00
347      */

348     static public Timestamp addDays (Timestamp day, int offset)
349     {
350         if (day == null)
351             return null;
352         //
353
GregorianCalendar cal = new GregorianCalendar();
354         cal.setTime(day);
355         cal.set(Calendar.HOUR_OF_DAY, 0);
356         cal.set(Calendar.MINUTE, 0);
357         cal.set(Calendar.SECOND, 0);
358         cal.set(Calendar.MILLISECOND, 0);
359         if (offset == 0)
360             return new Timestamp (cal.getTimeInMillis());
361         cal.add(Calendar.DAY_OF_YEAR, offset); // may have a problem with negative (before 1/1)
362
return new Timestamp (cal.getTimeInMillis());
363     } // addDays
364

365     /**
366      * Return DateTime + offset in minutes
367      * @param dateTime Date and Time
368      * @param offset minute offset
369      * @return dateTime + offset in minutes
370      */

371     static public Timestamp addMinutess (Timestamp dateTime, int offset)
372     {
373         if (dateTime == null)
374             return null;
375         if (offset == 0)
376             return dateTime;
377         //
378
GregorianCalendar cal = new GregorianCalendar();
379         cal.setTime(dateTime);
380         cal.add(Calendar.MINUTE, offset); // may have a problem with negative
381
return new Timestamp (cal.getTimeInMillis());
382     } // addMinutes
383

384     /*************************************************************************/
385     
386     /**
387      * Format Elapsed Time
388      * @param start start time
389      * @param end end time
390      * @return formatted time string 1'23:59:59.999
391      */

392     public static String JavaDoc formatElapsed (Timestamp start, Timestamp end)
393     {
394         if (start == null)
395             return "NoStartTime";
396         if (end == null)
397             return "NoEndTime";
398         long startTime = start.getTime();
399         long endTime = end.getTime();
400         return formatElapsed(endTime-startTime);
401     } // formatElapsed
402

403     /**
404      * Format Elapsed Time
405      * @param elapsed time in ms
406      * @return formatted time string 1'23:59:59.999
407      */

408     public static String JavaDoc formatElapsed (long elapsed)
409     {
410         long miliSeconds = elapsed%1000;
411         elapsed = elapsed / 1000;
412         long seconds = elapsed%60;
413         elapsed = elapsed / 60;
414         long minutes = elapsed%60;
415         elapsed = elapsed / 60;
416         long hours = elapsed%24;
417         long days = elapsed / 24;
418         //
419
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
420         if (days != 0)
421             sb.append(days).append("'");
422         if (hours != 0)
423             sb.append(get2digits(hours)).append(":");
424         if (minutes != 0)
425             sb.append(get2digits(minutes)).append(":");
426         sb.append(get2digits(seconds)).append(".").append(miliSeconds);
427         return sb.toString();
428     } // format Elapsed
429

430     /**
431      * Get Minimum of 2 digits
432      * @param no number
433      * @return String
434      */

435     private static String JavaDoc get2digits (long no)
436     {
437         String JavaDoc s = String.valueOf(no);
438         if (s.length() > 1)
439             return s;
440         return "0" + s;
441     } // get2digits
442

443 } // TimeUtil
444
Popular Tags