KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilDateTime


1 /*
2  * $Id: UtilDateTime.java 6520 2006-01-17 17:02:40Z jaz $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.text.DecimalFormat JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Calendar JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * Utility class for handling java.util.Date, the java.sql data/time classes and related
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
40  * @author <a HREF="mailto:johan@ibibi.com">Johan Isacsson</a>
41  * @version $Rev: 6520 $
42  * @since 2.0
43  */

44 public class UtilDateTime {
45     public static final String JavaDoc[] months = {// // to be translated over CommonMonthName, see example in accounting
46
"January", "February", "March", "April", "May", "June",
47         "July", "August", "September", "October", "November",
48         "December"
49     };
50
51     public static final String JavaDoc[] days = {// to be translated over CommonDayName, see example in accounting
52
"Monday", "Tuesday", "Wednesday",
53         "Thursday", "Friday", "Saturday", "Sunday"
54     };
55
56     public static final String JavaDoc[][] timevals = {
57         {"1000", "millisecond"},
58         {"60", "second"},
59         {"60", "minute"},
60         {"24", "hour"},
61         {"168", "week"}
62     };
63
64     public static final DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("0.00;-0.00");
65
66     public static double getInterval(Date JavaDoc from, Date JavaDoc thru) {
67         return thru != null ? thru.getTime() - from.getTime() : 0;
68     }
69
70     public static double getInterval(Timestamp JavaDoc from, Timestamp JavaDoc thru) {
71         return thru != null ? thru.getTime() - from.getTime() + (thru.getNanos() - from.getNanos()) / 1000000 : 0;
72     }
73
74     public static String JavaDoc formatInterval(Date JavaDoc from, Date JavaDoc thru, int count, Locale JavaDoc locale) {
75         return formatInterval(getInterval(from, thru), count, locale);
76     }
77
78     public static String JavaDoc formatInterval(Date JavaDoc from, Date JavaDoc thru, Locale JavaDoc locale) {
79         return formatInterval(from, thru, 2, locale);
80     }
81
82     public static String JavaDoc formatInterval(Timestamp JavaDoc from, Timestamp JavaDoc thru, int count, Locale JavaDoc locale) {
83         return formatInterval(getInterval(from, thru), count, locale);
84     }
85
86     public static String JavaDoc formatInterval(Timestamp JavaDoc from, Timestamp JavaDoc thru, Locale JavaDoc locale) {
87         return formatInterval(from, thru, 2, locale);
88     }
89
90     public static String JavaDoc formatInterval(long interval, int count, Locale JavaDoc locale) {
91         return formatInterval((double) interval, count, locale);
92     }
93
94     public static String JavaDoc formatInterval(long interval, Locale JavaDoc locale) {
95         return formatInterval(interval, 2, locale);
96     }
97
98     public static String JavaDoc formatInterval(double interval, Locale JavaDoc locale) {
99         return formatInterval(interval, 2, locale);
100     }
101
102     public static String JavaDoc formatInterval(double interval, int count, Locale JavaDoc locale) {
103         ArrayList JavaDoc parts = new ArrayList JavaDoc(timevals.length);
104         for (int i = 0; i < timevals.length; i++) {
105             int value = Integer.parseInt(timevals[i][0]);
106             double remainder = interval % value;
107             interval = interval / value;
108             parts.add(new Double JavaDoc(remainder));
109         }
110
111         Map JavaDoc uiDateTimeMap = UtilProperties.getResourceBundleMap("DateTimeLabels", locale);
112
113         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114         for (int i = parts.size() - 1; i >= 0 && count > 0; i--) {
115             if (sb.length() > 0) sb.append(", ");
116             Double JavaDoc D = (Double JavaDoc) parts.get(i);
117             double d = D.doubleValue();
118             if (d < 1) continue;
119             count--;
120             sb.append(count == 0 ? df.format(d) : Integer.toString(D.intValue()));
121             sb.append(' ');
122             String JavaDoc label;
123             if (D.intValue() == 1) {
124                 label = (String JavaDoc) uiDateTimeMap.get(timevals[i][1] + ".singular");
125             } else {
126                 label = (String JavaDoc) uiDateTimeMap.get(timevals[i][1] + ".plural");
127             }
128             sb.append(label);
129         }
130         return sb.toString();
131     }
132
133     /**
134      * Return a Timestamp for right now
135      *
136      * @return Timestamp for right now
137      */

138     public static java.sql.Timestamp JavaDoc nowTimestamp() {
139         return getTimestamp(System.currentTimeMillis());
140     }
141
142     public static java.sql.Timestamp JavaDoc getTimestamp(long time) {
143         return new java.sql.Timestamp JavaDoc(time);
144     }
145
146     /**
147      * Return a string formatted as yyyyMMddHHmmss
148      *
149      * @return String formatted for right now
150      */

151     public static String JavaDoc nowDateString() {
152         return nowDateString("yyyyMMddHHmmss");
153     }
154
155     /**
156      * Return a string formatted as format
157      *
158      * @return String formatted for right now
159      */

160     public static String JavaDoc nowDateString(String JavaDoc format) {
161         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(format);
162         return df.format(new Date JavaDoc());
163     }
164
165     /**
166      * Return a Date for right now
167      *
168      * @return Date for right now
169      */

170     public static java.util.Date JavaDoc nowDate() {
171         return new java.util.Date JavaDoc();
172     }
173
174     public static java.sql.Timestamp JavaDoc getDayStart(java.sql.Timestamp JavaDoc stamp) {
175         return getDayStart(stamp, 0);
176     }
177
178     public static java.sql.Timestamp JavaDoc getDayStart(java.sql.Timestamp JavaDoc stamp, int daysLater) {
179         Calendar JavaDoc tempCal = Calendar.getInstance();
180         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
181         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
182         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
183         java.sql.Timestamp JavaDoc retStamp = new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
184         retStamp.setNanos(0);
185         return retStamp;
186     }
187
188     public static java.sql.Timestamp JavaDoc getNextDayStart(java.sql.Timestamp JavaDoc stamp) {
189         return getDayStart(stamp, 1);
190     }
191
192     public static java.sql.Timestamp JavaDoc getDayEnd(java.sql.Timestamp JavaDoc stamp) {
193         return getDayEnd(stamp, 0);
194     }
195
196     public static java.sql.Timestamp JavaDoc getDayEnd(java.sql.Timestamp JavaDoc stamp, int daysLater) {
197         Calendar JavaDoc tempCal = Calendar.getInstance();
198         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
199         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
200         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
201         java.sql.Timestamp JavaDoc retStamp = new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
202         retStamp.setNanos(999999999);
203         return retStamp;
204     }
205
206     /**
207      * Return the date for the first day of the year
208      *
209      * @param stamp
210      * @return java.sql.Timestamp
211      */

212     public static java.sql.Timestamp JavaDoc getYearStart(java.sql.Timestamp JavaDoc stamp) {
213         return getYearStart(stamp, 0, 0, 0);
214     }
215
216     public static java.sql.Timestamp JavaDoc getYearStart(java.sql.Timestamp JavaDoc stamp, int daysLater) {
217         return getYearStart(stamp, daysLater, 0, 0);
218     }
219
220     public static java.sql.Timestamp JavaDoc getYearStart(java.sql.Timestamp JavaDoc stamp, int daysLater, int yearsLater) {
221         return getYearStart(stamp, daysLater, 0, yearsLater);
222     }
223     public static java.sql.Timestamp JavaDoc getYearStart(java.sql.Timestamp JavaDoc stamp, int daysLater, int monthsLater, int yearsLater) {
224         Calendar JavaDoc tempCal = Calendar.getInstance();
225         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
226         tempCal.set(tempCal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0);
227         tempCal.add(Calendar.YEAR, yearsLater);
228         tempCal.add(Calendar.MONTH, monthsLater);
229         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
230         java.sql.Timestamp JavaDoc retStamp = new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
231         retStamp.setNanos(0);
232         return retStamp;
233     }
234     public static java.sql.Timestamp JavaDoc getYearStart(java.sql.Timestamp JavaDoc stamp, Number JavaDoc daysLater, Number JavaDoc monthsLater, Number JavaDoc yearsLater) {
235         return getYearStart(stamp, (daysLater == null ? 0 : daysLater.intValue()),
236                 (monthsLater == null ? 0 : monthsLater.intValue()), (yearsLater == null ? 0 : yearsLater.intValue()));
237     }
238
239     /**
240      * Return the date for the first day of the month
241      *
242      * @param stamp
243      * @return java.sql.Timestamp
244      */

245     public static java.sql.Timestamp JavaDoc getMonthStart(java.sql.Timestamp JavaDoc stamp) {
246         return getMonthStart(stamp, 0, 0);
247     }
248
249     public static java.sql.Timestamp JavaDoc getMonthStart(java.sql.Timestamp JavaDoc stamp, int daysLater) {
250         return getMonthStart(stamp, daysLater, 0);
251     }
252
253     public static java.sql.Timestamp JavaDoc getMonthStart(java.sql.Timestamp JavaDoc stamp, int daysLater, int monthsLater) {
254         Calendar JavaDoc tempCal = Calendar.getInstance();
255         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
256         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), 1, 0, 0, 0);
257         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
258         tempCal.add(Calendar.MONTH, monthsLater);
259         java.sql.Timestamp JavaDoc retStamp = new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
260         retStamp.setNanos(0);
261         return retStamp;
262     }
263
264     /**
265      * Return the date for the first day of the week
266      *
267      * @param stamp
268      * @return java.sql.Timestamp
269      */

270     public static java.sql.Timestamp JavaDoc getWeekStart(java.sql.Timestamp JavaDoc stamp) {
271         return getWeekStart(stamp, 0, 0);
272     }
273
274     public static java.sql.Timestamp JavaDoc getWeekStart(java.sql.Timestamp JavaDoc stamp, int daysLater) {
275         return getWeekStart(stamp, daysLater, 0);
276     }
277
278     public static java.sql.Timestamp JavaDoc getWeekStart(java.sql.Timestamp JavaDoc stamp, int daysLater, int weeksLater) {
279         Calendar JavaDoc tempCal = Calendar.getInstance();
280         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
281         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
282         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
283         tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
284         tempCal.add(Calendar.WEEK_OF_MONTH, weeksLater);
285         java.sql.Timestamp JavaDoc retStamp = new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
286         retStamp.setNanos(0);
287         return retStamp;
288     }
289
290     public static java.sql.Timestamp JavaDoc getWeekEnd(java.sql.Timestamp JavaDoc stamp) {
291         Calendar JavaDoc tempCal = Calendar.getInstance();
292         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
293         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
294         tempCal.add(Calendar.WEEK_OF_MONTH, 1);
295         tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
296         tempCal.add(Calendar.SECOND, -1);
297         java.sql.Timestamp JavaDoc retStamp = new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
298         retStamp.setNanos(0);
299         return retStamp;
300     }
301     
302     public static java.util.Calendar JavaDoc toCalendar(java.sql.Timestamp JavaDoc stamp) {
303         Calendar JavaDoc cal = Calendar.getInstance();
304         if (stamp != null) {
305             cal.setTimeInMillis(stamp.getTime());
306         }
307         return cal;
308     }
309
310     /**
311      * Converts a date String into a java.sql.Date
312      *
313      * @param date The date String: MM/DD/YYYY
314      * @return A java.sql.Date made from the date String
315      */

316     public static java.sql.Date JavaDoc toSqlDate(String JavaDoc date) {
317         java.util.Date JavaDoc newDate = toDate(date, "00:00:00");
318
319         if (newDate != null) {
320             return new java.sql.Date JavaDoc(newDate.getTime());
321         } else {
322             return null;
323         }
324     }
325
326     /**
327      * Makes a java.sql.Date from separate Strings for month, day, year
328      *
329      * @param monthStr The month String
330      * @param dayStr The day String
331      * @param yearStr The year String
332      * @return A java.sql.Date made from separate Strings for month, day, year
333      */

334     public static java.sql.Date JavaDoc toSqlDate(String JavaDoc monthStr, String JavaDoc dayStr, String JavaDoc yearStr) {
335         java.util.Date JavaDoc newDate = toDate(monthStr, dayStr, yearStr, "0", "0", "0");
336
337         if (newDate != null) {
338             return new java.sql.Date JavaDoc(newDate.getTime());
339         } else {
340             return null;
341         }
342     }
343
344     /**
345      * Makes a java.sql.Date from separate ints for month, day, year
346      *
347      * @param month The month int
348      * @param day The day int
349      * @param year The year int
350      * @return A java.sql.Date made from separate ints for month, day, year
351      */

352     public static java.sql.Date JavaDoc toSqlDate(int month, int day, int year) {
353         java.util.Date JavaDoc newDate = toDate(month, day, year, 0, 0, 0);
354
355         if (newDate != null) {
356             return new java.sql.Date JavaDoc(newDate.getTime());
357         } else {
358             return null;
359         }
360     }
361
362     /**
363      * Converts a time String into a java.sql.Time
364      *
365      * @param time The time String: either HH:MM or HH:MM:SS
366      * @return A java.sql.Time made from the time String
367      */

368     public static java.sql.Time JavaDoc toSqlTime(String JavaDoc time) {
369         java.util.Date JavaDoc newDate = toDate("1/1/1970", time);
370
371         if (newDate != null) {
372             return new java.sql.Time JavaDoc(newDate.getTime());
373         } else {
374             return null;
375         }
376     }
377
378     /**
379      * Makes a java.sql.Time from separate Strings for hour, minute, and second.
380      *
381      * @param hourStr The hour String
382      * @param minuteStr The minute String
383      * @param secondStr The second String
384      * @return A java.sql.Time made from separate Strings for hour, minute, and second.
385      */

386     public static java.sql.Time JavaDoc toSqlTime(String JavaDoc hourStr, String JavaDoc minuteStr, String JavaDoc secondStr) {
387         java.util.Date JavaDoc newDate = toDate("0", "0", "0", hourStr, minuteStr, secondStr);
388
389         if (newDate != null) {
390             return new java.sql.Time JavaDoc(newDate.getTime());
391         } else {
392             return null;
393         }
394     }
395
396     /**
397      * Makes a java.sql.Time from separate ints for hour, minute, and second.
398      *
399      * @param hour The hour int
400      * @param minute The minute int
401      * @param second The second int
402      * @return A java.sql.Time made from separate ints for hour, minute, and second.
403      */

404     public static java.sql.Time JavaDoc toSqlTime(int hour, int minute, int second) {
405         java.util.Date JavaDoc newDate = toDate(0, 0, 0, hour, minute, second);
406
407         if (newDate != null) {
408             return new java.sql.Time JavaDoc(newDate.getTime());
409         } else {
410             return null;
411         }
412     }
413
414     /**
415      * Converts a date and time String into a Timestamp
416      *
417      * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
418      * @return The corresponding Timestamp
419      */

420     public static java.sql.Timestamp JavaDoc toTimestamp(String JavaDoc dateTime) {
421         java.util.Date JavaDoc newDate = toDate(dateTime);
422
423         if (newDate != null) {
424             return new java.sql.Timestamp JavaDoc(newDate.getTime());
425         } else {
426             return null;
427         }
428     }
429
430     /**
431      * Converts a date String and a time String into a Timestamp
432      *
433      * @param date The date String: MM/DD/YYYY
434      * @param time The time String: either HH:MM or HH:MM:SS
435      * @return A Timestamp made from the date and time Strings
436      */

437     public static java.sql.Timestamp JavaDoc toTimestamp(String JavaDoc date, String JavaDoc time) {
438         java.util.Date JavaDoc newDate = toDate(date, time);
439
440         if (newDate != null) {
441             return new java.sql.Timestamp JavaDoc(newDate.getTime());
442         } else {
443             return null;
444         }
445     }
446
447     /**
448      * Makes a Timestamp from separate Strings for month, day, year, hour, minute, and second.
449      *
450      * @param monthStr The month String
451      * @param dayStr The day String
452      * @param yearStr The year String
453      * @param hourStr The hour String
454      * @param minuteStr The minute String
455      * @param secondStr The second String
456      * @return A Timestamp made from separate Strings for month, day, year, hour, minute, and second.
457      */

458     public static java.sql.Timestamp JavaDoc toTimestamp(String JavaDoc monthStr, String JavaDoc dayStr, String JavaDoc yearStr, String JavaDoc hourStr,
459             String JavaDoc minuteStr, String JavaDoc secondStr) {
460         java.util.Date JavaDoc newDate = toDate(monthStr, dayStr, yearStr, hourStr, minuteStr, secondStr);
461
462         if (newDate != null) {
463             return new java.sql.Timestamp JavaDoc(newDate.getTime());
464         } else {
465             return null;
466         }
467     }
468
469     /**
470      * Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.
471      *
472      * @param month The month int
473      * @param day The day int
474      * @param year The year int
475      * @param hour The hour int
476      * @param minute The minute int
477      * @param second The second int
478      * @return A Timestamp made from separate ints for month, day, year, hour, minute, and second.
479      */

480     public static java.sql.Timestamp JavaDoc toTimestamp(int month, int day, int year, int hour, int minute, int second) {
481         java.util.Date JavaDoc newDate = toDate(month, day, year, hour, minute, second);
482
483         if (newDate != null) {
484             return new java.sql.Timestamp JavaDoc(newDate.getTime());
485         } else {
486             return null;
487         }
488     }
489
490     public static java.sql.Timestamp JavaDoc toTimestamp(Date JavaDoc date) {
491         if (date == null) return null;
492         return new Timestamp JavaDoc(date.getTime());
493     }
494
495     /**
496      * Converts a date and time String into a Date
497      *
498      * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
499      * @return The corresponding Date
500      */

501     public static java.util.Date JavaDoc toDate(String JavaDoc dateTime) {
502         if (dateTime == null) {
503             return null;
504         }
505         // dateTime must have one space between the date and time...
506
String JavaDoc date = dateTime.substring(0, dateTime.indexOf(" "));
507         String JavaDoc time = dateTime.substring(dateTime.indexOf(" ") + 1);
508
509         return toDate(date, time);
510     }
511
512     /**
513      * Converts a date String and a time String into a Date
514      *
515      * @param date The date String: MM/DD/YYYY
516      * @param time The time String: either HH:MM or HH:MM:SS
517      * @return A Date made from the date and time Strings
518      */

519     public static java.util.Date JavaDoc toDate(String JavaDoc date, String JavaDoc time) {
520         if (date == null || time == null) return null;
521         String JavaDoc month;
522         String JavaDoc day;
523         String JavaDoc year;
524         String JavaDoc hour;
525         String JavaDoc minute;
526         String JavaDoc second;
527
528         int dateSlash1 = date.indexOf("/");
529         int dateSlash2 = date.lastIndexOf("/");
530
531         if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null;
532         int timeColon1 = time.indexOf(":");
533         int timeColon2 = time.lastIndexOf(":");
534
535         if (timeColon1 <= 0) return null;
536         month = date.substring(0, dateSlash1);
537         day = date.substring(dateSlash1 + 1, dateSlash2);
538         year = date.substring(dateSlash2 + 1);
539         hour = time.substring(0, timeColon1);
540
541         if (timeColon1 == timeColon2) {
542             minute = time.substring(timeColon1 + 1);
543             second = "0";
544         } else {
545             minute = time.substring(timeColon1 + 1, timeColon2);
546             second = time.substring(timeColon2 + 1);
547         }
548
549         return toDate(month, day, year, hour, minute, second);
550     }
551
552     /**
553      * Makes a Date from separate Strings for month, day, year, hour, minute, and second.
554      *
555      * @param monthStr The month String
556      * @param dayStr The day String
557      * @param yearStr The year String
558      * @param hourStr The hour String
559      * @param minuteStr The minute String
560      * @param secondStr The second String
561      * @return A Date made from separate Strings for month, day, year, hour, minute, and second.
562      */

563     public static java.util.Date JavaDoc toDate(String JavaDoc monthStr, String JavaDoc dayStr, String JavaDoc yearStr, String JavaDoc hourStr,
564             String JavaDoc minuteStr, String JavaDoc secondStr) {
565         int month, day, year, hour, minute, second;
566
567         try {
568             month = Integer.parseInt(monthStr);
569             day = Integer.parseInt(dayStr);
570             year = Integer.parseInt(yearStr);
571             hour = Integer.parseInt(hourStr);
572             minute = Integer.parseInt(minuteStr);
573             second = Integer.parseInt(secondStr);
574         } catch (Exception JavaDoc e) {
575             return null;
576         }
577         return toDate(month, day, year, hour, minute, second);
578     }
579
580     /**
581      * Makes a Date from separate ints for month, day, year, hour, minute, and second.
582      *
583      * @param month The month int
584      * @param day The day int
585      * @param year The year int
586      * @param hour The hour int
587      * @param minute The minute int
588      * @param second The second int
589      * @return A Date made from separate ints for month, day, year, hour, minute, and second.
590      */

591     public static java.util.Date JavaDoc toDate(int month, int day, int year, int hour, int minute, int second) {
592         Calendar JavaDoc calendar = Calendar.getInstance();
593
594         try {
595             calendar.set(year, month - 1, day, hour, minute, second);
596         } catch (Exception JavaDoc e) {
597             return null;
598         }
599         return new java.util.Date JavaDoc(calendar.getTime().getTime());
600     }
601
602     /**
603      * Makes a date String in the given from a Date
604      *
605      * @param date The Date
606      * @return A date String in the given format
607      */

608     public static String JavaDoc toDateString(java.util.Date JavaDoc date, String JavaDoc format) {
609         if (date == null) return "";
610         SimpleDateFormat JavaDoc dateFormat = null;
611         if (format != null) {
612             dateFormat = new SimpleDateFormat JavaDoc(format);
613         } else {
614             dateFormat = new SimpleDateFormat JavaDoc();
615         }
616
617         Calendar JavaDoc calendar = Calendar.getInstance();
618
619         calendar.setTime(date);
620         return dateFormat.format(date);
621     }
622     /**
623      * Makes a date String in the format MM/DD/YYYY from a Date
624      *
625      * @param date The Date
626      * @return A date String in the format MM/DD/YYYY
627      */

628     public static String JavaDoc toDateString(java.util.Date JavaDoc date) {
629         return toDateString(date, "MM/dd/yyyy");
630     }
631
632     /**
633      * Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.
634      *
635      * @param date The Date
636      * @return A time String in the format HH:MM:SS or HH:MM
637      */

638     public static String JavaDoc toTimeString(java.util.Date JavaDoc date) {
639         if (date == null) return "";
640         Calendar JavaDoc calendar = Calendar.getInstance();
641
642         calendar.setTime(date);
643         return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));
644     }
645
646     /**
647      * Makes a time String in the format HH:MM:SS from a separate ints for hour, minute, and second. If the seconds are 0, then the output is in HH:MM.
648      *
649      * @param hour The hour int
650      * @param minute The minute int
651      * @param second The second int
652      * @return A time String in the format HH:MM:SS or HH:MM
653      */

654     public static String JavaDoc toTimeString(int hour, int minute, int second) {
655         String JavaDoc hourStr;
656         String JavaDoc minuteStr;
657         String JavaDoc secondStr;
658
659         if (hour < 10) {
660             hourStr = "0" + hour;
661         } else {
662             hourStr = "" + hour;
663         }
664         if (minute < 10) {
665             minuteStr = "0" + minute;
666         } else {
667             minuteStr = "" + minute;
668         }
669         if (second < 10) {
670             secondStr = "0" + second;
671         } else {
672             secondStr = "" + second;
673         }
674         if (second == 0) {
675             return hourStr + ":" + minuteStr;
676         } else {
677             return hourStr + ":" + minuteStr + ":" + secondStr;
678         }
679     }
680
681     /**
682      * Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date. If the seconds are 0 they are left off.
683      *
684      * @param date The Date
685      * @return A combined data and time string in the format "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are 0.
686      */

687     public static String JavaDoc toDateTimeString(java.util.Date JavaDoc date) {
688         if (date == null) return "";
689         String JavaDoc dateString = toDateString(date);
690         String JavaDoc timeString = toTimeString(date);
691
692         if (dateString != null && timeString != null) {
693             return dateString + " " + timeString;
694         } else {
695             return "";
696         }
697     }
698
699     /**
700      * Makes a Timestamp for the beginning of the month
701      *
702      * @return A Timestamp of the beginning of the month
703      */

704     public static java.sql.Timestamp JavaDoc monthBegin() {
705         Calendar JavaDoc mth = Calendar.getInstance();
706
707         mth.set(Calendar.DAY_OF_MONTH, 1);
708         mth.set(Calendar.HOUR_OF_DAY, 0);
709         mth.set(Calendar.MINUTE, 0);
710         mth.set(Calendar.SECOND, 0);
711         mth.set(Calendar.MILLISECOND, 0);
712         mth.set(Calendar.AM_PM, Calendar.AM);
713         return new java.sql.Timestamp JavaDoc(mth.getTime().getTime());
714     }
715
716     /**
717      * returns a week number in a year for a Timestamp input
718      *
719      * @param input Timestamp date
720      * @return A int containing the week number
721      */

722     public static int weekNumber(Timestamp JavaDoc input) {
723         Timestamp JavaDoc yearStart = UtilDateTime.getYearStart(input);
724         Timestamp JavaDoc weekStart = UtilDateTime.getWeekStart(yearStart);
725
726         int days = 0;
727         for (days = 0; UtilDateTime.getDayStart(weekStart, days).compareTo(yearStart) == 0; days++) ;
728
729         // the splitted week belongs to the year where there are the most days (ISO)
730
Timestamp JavaDoc week1Start = weekStart;
731         if (days < 4)
732             week1Start = UtilDateTime.getWeekStart(weekStart, 7);
733
734         int weeks = 0;
735         for (weeks = 0; UtilDateTime.getDayStart(week1Start, weeks * 7).compareTo(input) < 0; weeks++) ;
736
737         return ++weeks; // start at 1
738
}
739 }
740
741
Popular Tags