KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > UtilDateTime


1 /*
2  * $Id: UtilDateTime.java,v 1.2 2005/01/31 05:27:55 jdon Exp $
3  *
4  * Copyright (c) 2001, 2002 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
25 package com.jdon.util;
26
27 import java.util.*;
28 import java.text.*;
29 import java.util.Date JavaDoc;
30
31 /**
32  * Utility class for handling java.util.Date, the java.sql data/time classes and related information
33  *
34  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
35  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36  * @version $Revision: 1.2 $
37  * @since 2.0
38  */

39 public class UtilDateTime {
40
41     /** Return a Timestamp for right now
42      * @return Timestamp for right now
43      */

44     public static java.sql.Timestamp JavaDoc nowTimestamp() {
45         return new java.sql.Timestamp JavaDoc(System.currentTimeMillis());
46     }
47
48     /** Return a Date for right now
49      * @return Date for right now
50      */

51     public static java.util.Date JavaDoc nowDate() {
52         return new java.util.Date JavaDoc();
53     }
54
55     public static java.sql.Timestamp JavaDoc getDayStart(java.sql.Timestamp JavaDoc stamp) {
56         return getDayStart(stamp, 0);
57     }
58
59     public static java.sql.Timestamp JavaDoc getDayStart(java.sql.Timestamp JavaDoc stamp, int daysLater) {
60         Calendar tempCal = Calendar.getInstance();
61
62         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
63         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
64         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
65         return new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
66     }
67
68     public static java.sql.Timestamp JavaDoc getNextDayStart(java.sql.Timestamp JavaDoc stamp) {
69         return getDayStart(stamp, 1);
70     }
71
72     public static java.sql.Timestamp JavaDoc getDayEnd(java.sql.Timestamp JavaDoc stamp) {
73         return getDayEnd(stamp, 0);
74     }
75
76     public static java.sql.Timestamp JavaDoc getDayEnd(java.sql.Timestamp JavaDoc stamp, int daysLater) {
77         Calendar tempCal = Calendar.getInstance();
78
79         tempCal.setTime(new java.util.Date JavaDoc(stamp.getTime()));
80         tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
81         tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
82         return new java.sql.Timestamp JavaDoc(tempCal.getTime().getTime());
83     }
84
85     /** Converts a date String into a java.sql.Date
86      * @param date The date String: MM/DD/YYYY
87      * @return A java.sql.Date made from the date String
88      */

89     public static java.sql.Date JavaDoc toSqlDate(String JavaDoc date) {
90         java.util.Date JavaDoc newDate = toDate(date, "00:00:00");
91
92         if (newDate != null)
93             return new java.sql.Date JavaDoc(newDate.getTime());
94         else
95             return null;
96     }
97
98     /** Makes a java.sql.Date from separate Strings for month, day, year
99      * @param monthStr The month String
100      * @param dayStr The day String
101      * @param yearStr The year String
102      * @return A java.sql.Date made from separate Strings for month, day, year
103      */

104     public static java.sql.Date JavaDoc toSqlDate(String JavaDoc monthStr, String JavaDoc dayStr, String JavaDoc yearStr) {
105         java.util.Date JavaDoc newDate = toDate(monthStr, dayStr, yearStr, "0", "0", "0");
106
107         if (newDate != null)
108             return new java.sql.Date JavaDoc(newDate.getTime());
109         else
110             return null;
111     }
112
113     /** Makes a java.sql.Date from separate ints for month, day, year
114      * @param month The month int
115      * @param day The day int
116      * @param year The year int
117      * @return A java.sql.Date made from separate ints for month, day, year
118      */

119     public static java.sql.Date JavaDoc toSqlDate(int month, int day, int year) {
120         java.util.Date JavaDoc newDate = toDate(month, day, year, 0, 0, 0);
121
122         if (newDate != null)
123             return new java.sql.Date JavaDoc(newDate.getTime());
124         else
125             return null;
126     }
127
128     /** Converts a time String into a java.sql.Time
129      * @param time The time String: either HH:MM or HH:MM:SS
130      * @return A java.sql.Time made from the time String
131      */

132     public static java.sql.Time JavaDoc toSqlTime(String JavaDoc time) {
133         java.util.Date JavaDoc newDate = toDate("1/1/1970", time);
134
135         if (newDate != null)
136             return new java.sql.Time JavaDoc(newDate.getTime());
137         else
138             return null;
139     }
140
141     /** Makes a java.sql.Time from separate Strings for hour, minute, and second.
142      * @param hourStr The hour String
143      * @param minuteStr The minute String
144      * @param secondStr The second String
145      * @return A java.sql.Time made from separate Strings for hour, minute, and second.
146      */

147     public static java.sql.Time JavaDoc toSqlTime(String JavaDoc hourStr, String JavaDoc minuteStr, String JavaDoc secondStr) {
148         java.util.Date JavaDoc newDate = toDate("0", "0", "0", hourStr, minuteStr, secondStr);
149
150         if (newDate != null)
151             return new java.sql.Time JavaDoc(newDate.getTime());
152         else
153             return null;
154     }
155
156     /** Makes a java.sql.Time from separate ints for hour, minute, and second.
157      * @param hour The hour int
158      * @param minute The minute int
159      * @param second The second int
160      * @return A java.sql.Time made from separate ints for hour, minute, and second.
161      */

162     public static java.sql.Time JavaDoc toSqlTime(int hour, int minute, int second) {
163         java.util.Date JavaDoc newDate = toDate(0, 0, 0, hour, minute, second);
164
165         if (newDate != null)
166             return new java.sql.Time JavaDoc(newDate.getTime());
167         else
168             return null;
169     }
170
171     /** Converts a date and time String into a Timestamp
172      * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
173      * @return The corresponding Timestamp
174      */

175     public static java.sql.Timestamp JavaDoc toTimestamp(String JavaDoc dateTime) {
176         java.util.Date JavaDoc newDate = toDate(dateTime);
177
178         if (newDate != null)
179             return new java.sql.Timestamp JavaDoc(newDate.getTime());
180         else
181             return null;
182     }
183
184     /** Converts a date String and a time String into a Timestamp
185      * @param date The date String: MM/DD/YYYY
186      * @param time The time String: either HH:MM or HH:MM:SS
187      * @return A Timestamp made from the date and time Strings
188      */

189     public static java.sql.Timestamp JavaDoc toTimestamp(String JavaDoc date, String JavaDoc time) {
190         java.util.Date JavaDoc newDate = toDate(date, time);
191
192         if (newDate != null)
193             return new java.sql.Timestamp JavaDoc(newDate.getTime());
194         else
195             return null;
196     }
197
198     /** Makes a Timestamp from separate Strings for month, day, year, hour, minute, and second.
199      * @param monthStr The month String
200      * @param dayStr The day String
201      * @param yearStr The year String
202      * @param hourStr The hour String
203      * @param minuteStr The minute String
204      * @param secondStr The second String
205      * @return A Timestamp made from separate Strings for month, day, year, hour, minute, and second.
206      */

207     public static java.sql.Timestamp JavaDoc toTimestamp(String JavaDoc monthStr, String JavaDoc dayStr, String JavaDoc yearStr, String JavaDoc hourStr,
208         String JavaDoc minuteStr, String JavaDoc secondStr) {
209         java.util.Date JavaDoc newDate = toDate(monthStr, dayStr, yearStr, hourStr, minuteStr, secondStr);
210
211         if (newDate != null)
212             return new java.sql.Timestamp JavaDoc(newDate.getTime());
213         else
214             return null;
215     }
216
217     /** Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.
218      * @param month The month int
219      * @param day The day int
220      * @param year The year int
221      * @param hour The hour int
222      * @param minute The minute int
223      * @param second The second int
224      * @return A Timestamp made from separate ints for month, day, year, hour, minute, and second.
225      */

226     public static java.sql.Timestamp JavaDoc toTimestamp(int month, int day, int year, int hour, int minute, int second) {
227         java.util.Date JavaDoc newDate = toDate(month, day, year, hour, minute, second);
228
229         if (newDate != null)
230             return new java.sql.Timestamp JavaDoc(newDate.getTime());
231         else
232             return null;
233     }
234
235     /** Converts a date and time String into a Date
236      * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
237      * @return The corresponding Date
238      */

239     public static java.util.Date JavaDoc toDate(String JavaDoc dateTime) {
240         // dateTime must have one space between the date and time...
241
String JavaDoc date = dateTime.substring(0, dateTime.indexOf(" "));
242         String JavaDoc time = dateTime.substring(dateTime.indexOf(" ") + 1);
243
244         return toDate(date, time);
245     }
246
247     /** Converts a date String and a time String into a Date
248      * @param date The date String: MM/DD/YYYY
249      * @param time The time String: either HH:MM or HH:MM:SS
250      * @return A Date made from the date and time Strings
251      */

252     public static java.util.Date JavaDoc toDate(String JavaDoc date, String JavaDoc time) {
253         if (date == null || time == null) return null;
254         String JavaDoc month;
255         String JavaDoc day;
256         String JavaDoc year;
257         String JavaDoc hour;
258         String JavaDoc minute;
259         String JavaDoc second;
260
261         int dateSlash1 = date.indexOf("/");
262         int dateSlash2 = date.lastIndexOf("/");
263
264         if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null;
265         int timeColon1 = time.indexOf(":");
266         int timeColon2 = time.lastIndexOf(":");
267
268         if (timeColon1 <= 0) return null;
269         month = date.substring(0, dateSlash1);
270         day = date.substring(dateSlash1 + 1, dateSlash2);
271         year = date.substring(dateSlash2 + 1);
272         hour = time.substring(0, timeColon1);
273
274         if (timeColon1 == timeColon2) {
275             minute = time.substring(timeColon1 + 1);
276             second = "0";
277         } else {
278             minute = time.substring(timeColon1 + 1, timeColon2);
279             second = time.substring(timeColon2 + 1);
280         }
281
282         return toDate(month, day, year, hour, minute, second);
283     }
284
285     /** Makes a Date from separate Strings for month, day, year, hour, minute, and second.
286      * @param monthStr The month String
287      * @param dayStr The day String
288      * @param yearStr The year String
289      * @param hourStr The hour String
290      * @param minuteStr The minute String
291      * @param secondStr The second String
292      * @return A Date made from separate Strings for month, day, year, hour, minute, and second.
293      */

294     public static java.util.Date JavaDoc toDate(String JavaDoc monthStr, String JavaDoc dayStr, String JavaDoc yearStr, String JavaDoc hourStr,
295         String JavaDoc minuteStr, String JavaDoc secondStr) {
296         int month, day, year, hour, minute, second;
297
298         try {
299             month = Integer.parseInt(monthStr);
300             day = Integer.parseInt(dayStr);
301             year = Integer.parseInt(yearStr);
302             hour = Integer.parseInt(hourStr);
303             minute = Integer.parseInt(minuteStr);
304             second = Integer.parseInt(secondStr);
305         } catch (Exception JavaDoc e) {
306             return null;
307         }
308         return toDate(month, day, year, hour, minute, second);
309     }
310
311     /** Makes a Date from separate ints for month, day, year, hour, minute, and second.
312      * @param month The month int
313      * @param day The day int
314      * @param year The year int
315      * @param hour The hour int
316      * @param minute The minute int
317      * @param second The second int
318      * @return A Date made from separate ints for month, day, year, hour, minute, and second.
319      */

320     public static java.util.Date JavaDoc toDate(int month, int day, int year, int hour, int minute, int second) {
321         Calendar calendar = Calendar.getInstance();
322
323         try {
324             calendar.set(year, month - 1, day, hour, minute, second);
325         } catch (Exception JavaDoc e) {
326             return null;
327         }
328         return new java.util.Date JavaDoc(calendar.getTime().getTime());
329     }
330
331     /** Makes a date String in the format MM/DD/YYYY from a Date
332      * @param date The Date
333      * @return A date String in the format MM/DD/YYYY
334      */

335     public static String JavaDoc toDateString(java.util.Date JavaDoc date) {
336         if (date == null) return "";
337         Calendar calendar = Calendar.getInstance();
338
339         calendar.setTime(date);
340         int month = calendar.get(Calendar.MONTH) + 1;
341         int day = calendar.get(Calendar.DAY_OF_MONTH);
342         int year = calendar.get(Calendar.YEAR);
343         String JavaDoc monthStr;
344         String JavaDoc dayStr;
345         String JavaDoc yearStr;
346
347         if (month < 10) {
348             monthStr = "0" + month;
349         } else {
350             monthStr = "" + month;
351         }
352         if (day < 10) {
353             dayStr = "0" + day;
354         } else {
355             dayStr = "" + day;
356         }
357         yearStr = "" + year;
358         return monthStr + "/" + dayStr + "/" + yearStr;
359     }
360
361     /** 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.
362      * @param date The Date
363      * @return A time String in the format HH:MM:SS or HH:MM
364      */

365     public static String JavaDoc toTimeString(java.util.Date JavaDoc date) {
366         if (date == null) return "";
367         Calendar calendar = Calendar.getInstance();
368
369         calendar.setTime(date);
370         return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));
371     }
372
373     /** 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.
374      * @param hour The hour int
375      * @param minute The minute int
376      * @param second The second int
377      * @return A time String in the format HH:MM:SS or HH:MM
378      */

379     public static String JavaDoc toTimeString(int hour, int minute, int second) {
380         String JavaDoc hourStr;
381         String JavaDoc minuteStr;
382         String JavaDoc secondStr;
383
384         if (hour < 10) {
385             hourStr = "0" + hour;
386         } else {
387             hourStr = "" + hour;
388         }
389         if (minute < 10) {
390             minuteStr = "0" + minute;
391         } else {
392             minuteStr = "" + minute;
393         }
394         if (second < 10) {
395             secondStr = "0" + second;
396         } else {
397             secondStr = "" + second;
398         }
399         if (second == 0)
400             return hourStr + ":" + minuteStr;
401         else
402             return hourStr + ":" + minuteStr + ":" + secondStr;
403     }
404
405     /** 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.
406      * @param date The Date
407      * @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.
408      */

409     public static String JavaDoc toDateTimeString(java.util.Date JavaDoc date) {
410         if (date == null) return "";
411         String JavaDoc dateString = toDateString(date);
412         String JavaDoc timeString = toTimeString(date);
413
414         if (dateString != null && timeString != null)
415             return dateString + " " + timeString;
416         else
417             return "";
418     }
419
420     /** Makes a Timestamp for the beginning of the month
421      * @return A Timestamp of the beginning of the month
422      */

423     public static java.sql.Timestamp JavaDoc monthBegin() {
424         Calendar mth = Calendar.getInstance();
425
426         mth.set(Calendar.DAY_OF_MONTH, 1);
427         mth.set(Calendar.HOUR_OF_DAY, 0);
428         mth.set(Calendar.MINUTE, 0);
429         mth.set(Calendar.SECOND, 0);
430         mth.set(Calendar.AM_PM, Calendar.AM);
431         return new java.sql.Timestamp JavaDoc(mth.getTime().getTime());
432     }
433     
434     private static final char[] zeroArray = "0000000000000000".toCharArray();
435     
436     public static final String JavaDoc zeroPadString(String JavaDoc string, int length) {
437         if (string == null || string.length() > length) {
438             return string;
439         }
440         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(length);
441         buf.append(zeroArray, 0, length-string.length()).append(string);
442         return buf.toString();
443      }
444     
445      /**
446       * save the datetime of System.currentTimeMillis() to
447       * dtatbase persistence.
448       * @param now
449       * @return
450       */

451      public static final String JavaDoc dateToMillis(long now) {
452         return zeroPadString(Long.toString(now), 15);
453      }
454
455     /**
456      * datetime is the String of System.currentTimeMillis()
457      * 返回标准中国(缺çœ?)的时间显示格å¼?
458      *
459      */

460     public static String JavaDoc getDateTimeDisp(String JavaDoc datetime) {
461       if ((datetime == null) || (datetime.equals("")))
462         return "";
463
464       DateFormat formatter
465           = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
466       long datel = Long.parseLong(datetime);
467       return formatter.format(new Date JavaDoc(datel));
468
469     }
470 }
471
Popular Tags