KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > util > DateUtil


1 package org.appfuse.util;
2
3 import java.text.ParseException JavaDoc;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.GregorianCalendar JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.MissingResourceException JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.appfuse.Constants;
15 import org.springframework.context.i18n.LocaleContextHolder;
16
17
18 /**
19  * Date Utility Class
20  * This is used to convert Strings to Dates and Timestamps
21  *
22  * <p>
23  * <a HREF="DateUtil.java.htm"><i>View Source</i></a>
24  * </p>
25  *
26  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
27  * Modified by <a HREF="mailto:dan@getrolling.com">Dan Kibler </a>
28  * to correct time pattern. Minutes should be mm not MM
29  * (MM is month).
30  * @version $Revision: 1.7.2.1 $ $Date: 2006-10-03 12:58:45 -0600 (Tue, 03 Oct 2006) $
31  */

32 public class DateUtil {
33     //~ Static fields/initializers =============================================
34

35     private static Log log = LogFactory.getLog(DateUtil.class);
36     private static String JavaDoc defaultDatePattern = null;
37     private static String JavaDoc timePattern = "HH:mm";
38
39     //~ Methods ================================================================
40

41     /**
42      * Return default datePattern (MM/dd/yyyy)
43      * @return a string representing the date pattern on the UI
44      */

45     public static String JavaDoc getDatePattern() {
46         Locale JavaDoc locale = LocaleContextHolder.getLocale();
47         try {
48             defaultDatePattern = ResourceBundle.getBundle(Constants.BUNDLE_KEY, locale)
49                 .getString("date.format");
50         } catch (MissingResourceException JavaDoc mse) {
51             defaultDatePattern = "MM/dd/yyyy";
52         }
53         
54         return defaultDatePattern;
55     }
56     
57     public static String JavaDoc getDateTimePattern() {
58         return DateUtil.getDatePattern() + " HH:mm:ss.S";
59     }
60
61     /**
62      * This method attempts to convert an Oracle-formatted date
63      * in the form dd-MMM-yyyy to mm/dd/yyyy.
64      *
65      * @param aDate date from database as a string
66      * @return formatted string for the ui
67      */

68     public static final String JavaDoc getDate(Date JavaDoc aDate) {
69         SimpleDateFormat JavaDoc df = null;
70         String JavaDoc returnValue = "";
71
72         if (aDate != null) {
73             df = new SimpleDateFormat JavaDoc(getDatePattern());
74             returnValue = df.format(aDate);
75         }
76
77         return (returnValue);
78     }
79
80     /**
81      * This method generates a string representation of a date/time
82      * in the format you specify on input
83      *
84      * @param aMask the date pattern the string is in
85      * @param strDate a string representation of a date
86      * @return a converted Date object
87      * @see java.text.SimpleDateFormat
88      * @throws ParseException
89      */

90     public static final Date JavaDoc convertStringToDate(String JavaDoc aMask, String JavaDoc strDate)
91       throws ParseException JavaDoc {
92         SimpleDateFormat JavaDoc df = null;
93         Date JavaDoc date = null;
94         df = new SimpleDateFormat JavaDoc(aMask);
95
96         if (log.isDebugEnabled()) {
97             log.debug("converting '" + strDate + "' to date with mask '"
98                       + aMask + "'");
99         }
100
101         try {
102             date = df.parse(strDate);
103         } catch (ParseException JavaDoc pe) {
104             //log.error("ParseException: " + pe);
105
throw new ParseException JavaDoc(pe.getMessage(), pe.getErrorOffset());
106         }
107
108         return (date);
109     }
110
111     /**
112      * This method returns the current date time in the format:
113      * MM/dd/yyyy HH:MM a
114      *
115      * @param theTime the current time
116      * @return the current date/time
117      */

118     public static String JavaDoc getTimeNow(Date JavaDoc theTime) {
119         return getDateTime(timePattern, theTime);
120     }
121
122     /**
123      * This method returns the current date in the format: MM/dd/yyyy
124      *
125      * @return the current date
126      * @throws ParseException
127      */

128     public static Calendar JavaDoc getToday() throws ParseException JavaDoc {
129         Date JavaDoc today = new Date JavaDoc();
130         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(getDatePattern());
131
132         // This seems like quite a hack (date -> string -> date),
133
// but it works ;-)
134
String JavaDoc todayAsString = df.format(today);
135         Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
136         cal.setTime(convertStringToDate(todayAsString));
137
138         return cal;
139     }
140
141     /**
142      * This method generates a string representation of a date's date/time
143      * in the format you specify on input
144      *
145      * @param aMask the date pattern the string is in
146      * @param aDate a date object
147      * @return a formatted string representation of the date
148      *
149      * @see java.text.SimpleDateFormat
150      */

151     public static final String JavaDoc getDateTime(String JavaDoc aMask, Date JavaDoc aDate) {
152         SimpleDateFormat JavaDoc df = null;
153         String JavaDoc returnValue = "";
154
155         if (aDate == null) {
156             log.error("aDate is null!");
157         } else {
158             df = new SimpleDateFormat JavaDoc(aMask);
159             returnValue = df.format(aDate);
160         }
161
162         return (returnValue);
163     }
164
165     /**
166      * This method generates a string representation of a date based
167      * on the System Property 'dateFormat'
168      * in the format you specify on input
169      *
170      * @param aDate A date to convert
171      * @return a string representation of the date
172      */

173     public static final String JavaDoc convertDateToString(Date JavaDoc aDate) {
174         return getDateTime(getDatePattern(), aDate);
175     }
176
177     /**
178      * This method converts a String to a date using the datePattern
179      *
180      * @param strDate the date to convert (in format MM/dd/yyyy)
181      * @return a date object
182      *
183      * @throws ParseException
184      */

185     public static Date JavaDoc convertStringToDate(String JavaDoc strDate)
186       throws ParseException JavaDoc {
187         Date JavaDoc aDate = null;
188
189         try {
190             if (log.isDebugEnabled()) {
191                 log.debug("converting date with pattern: " + getDatePattern());
192             }
193
194             aDate = convertStringToDate(getDatePattern(), strDate);
195         } catch (ParseException JavaDoc pe) {
196             log.error("Could not convert '" + strDate
197                       + "' to a date, throwing exception");
198             pe.printStackTrace();
199             throw new ParseException JavaDoc(pe.getMessage(),
200                                      pe.getErrorOffset());
201                     
202         }
203
204         return aDate;
205     }
206 }
207
Popular Tags