View Javadoc

1   package com.panogenesis.util;
2   
3   import java.text.ParseException;
4   import java.text.SimpleDateFormat;
5   import java.util.Calendar;
6   import java.util.Date;
7   import java.util.GregorianCalendar;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  
13  /***
14   * Date Utility Class
15   * This is used to convert Strings to Dates and Timestamps
16   *
17   * <p>
18   * <a href="DateUtil.java.html"><i>View Source</i></a>
19   * </p>
20   * 
21   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
22   *  Modified by <a href="mailto:dan@getrolling.com">Dan Kibler </a> 
23   *   to correct time pattern. Minutes should be mm not MM
24   * 	(MM is month). 
25   * @version $Revision: 1.1 $ $Date: 2004/11/28 03:49:06 $
26   */
27  public class DateUtil {
28      //~ Static fields/initializers =============================================
29  
30      private static Log log = LogFactory.getLog(DateUtil.class);
31      private static String datePattern = "MM/dd/yyyy";
32      private static String timePattern = "HH:mm";
33  
34      //~ Methods ================================================================
35  
36      /***
37       * Return default datePattern (MM/dd/yyyy)
38       * @return a string representing the date pattern on the UI
39       */
40      public static String getDatePattern() {
41          return datePattern;
42      }
43  
44      /***
45       * This method attempts to convert an Oracle-formatted date
46       * in the form dd-MMM-yyyy to mm/dd/yyyy.
47       *
48       * @param aDate date from database as a string
49       * @return formatted string for the ui
50       */
51      public static final String getDate(Date aDate) {
52          SimpleDateFormat df = null;
53          String returnValue = "";
54  
55          if (aDate != null) {
56              df = new SimpleDateFormat(datePattern);
57              returnValue = df.format(aDate);
58          }
59  
60          return (returnValue);
61      }
62  
63      /***
64       * This method generates a string representation of a date/time
65       * in the format you specify on input
66       *
67       * @param aMask the date pattern the string is in
68       * @param strDate a string representation of a date
69       * @return a converted Date object
70       * @see java.text.SimpleDateFormat
71       * @throws ParseException
72       */
73      public static final Date convertStringToDate(String aMask, String strDate)
74        throws ParseException {
75          SimpleDateFormat df = null;
76          Date date = null;
77          df = new SimpleDateFormat(aMask);
78  
79          if (log.isDebugEnabled()) {
80              log.debug("converting '" + strDate + "' to date with mask '"
81                        + aMask + "'");
82          }
83  
84          try {
85              date = df.parse(strDate);
86          } catch (ParseException pe) {
87              //log.error("ParseException: " + pe);
88              throw new ParseException(pe.getMessage(), pe.getErrorOffset());
89          }
90  
91          return (date);
92      }
93  
94      /***
95       * This method returns the current date time in the format:
96       * MM/dd/yyyy HH:MM a
97       *
98       * @param theTime the current time
99       * @return the current date/time
100      */
101     public static String getTimeNow(Date theTime) {
102         return getDateTime(timePattern, theTime);
103     }
104 
105     /***
106      * This method returns the current date in the format: MM/dd/yyyy
107      * 
108      * @return the current date
109      * @throws ParseException
110      */
111     public static Calendar getToday() throws ParseException {
112         Date today = new Date();
113         SimpleDateFormat df = new SimpleDateFormat(datePattern);
114 
115         // This seems like quite a hack (date -> string -> date),
116         // but it works ;-)
117         String todayAsString = df.format(today);
118         Calendar cal = new GregorianCalendar();
119         cal.setTime(convertStringToDate(todayAsString));
120 
121         return cal;
122     }
123 
124     /***
125      * This method generates a string representation of a date's date/time
126      * in the format you specify on input
127      *
128      * @param aMask the date pattern the string is in
129      * @param aDate a date object
130      * @return a formatted string representation of the date
131      * 
132      * @see java.text.SimpleDateFormat
133      */
134     public static final String getDateTime(String aMask, Date aDate) {
135         SimpleDateFormat df = null;
136         String returnValue = "";
137 
138         if (aDate == null) {
139             log.error("aDate is null!");
140         } else {
141             df = new SimpleDateFormat(aMask);
142             returnValue = df.format(aDate);
143         }
144 
145         return (returnValue);
146     }
147 
148     /***
149      * This method generates a string representation of a date based
150      * on the System Property 'dateFormat'
151      * in the format you specify on input
152      * 
153      * @param aDate A date to convert
154      * @return a string representation of the date
155      */
156     public static final String convertDateToString(Date aDate) {
157         return getDateTime(datePattern, aDate);
158     }
159 
160     /***
161      * This method converts a String to a date using the datePattern
162      * 
163      * @param strDate the date to convert (in format MM/dd/yyyy)
164      * @return a date object
165      * 
166      * @throws ParseException
167      */
168     public static Date convertStringToDate(String strDate)
169       throws ParseException {
170         Date aDate = null;
171 
172         try {
173             if (log.isDebugEnabled()) {
174                 log.debug("converting date with pattern: " + datePattern);
175             }
176 
177             aDate = convertStringToDate(datePattern, strDate);
178         } catch (ParseException pe) {
179             log.error("Could not convert '" + strDate
180                       + "' to a date, throwing exception");
181             pe.printStackTrace();
182             throw new ParseException(pe.getMessage(),
183                                      pe.getErrorOffset());
184                     
185         }
186 
187         return aDate;
188     }
189 }