1 package org.appfuse.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 import java.util.Locale ; 9 import java.util.MissingResourceException ; 10 import java.util.ResourceBundle ; 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 32 public class DateUtil { 33 35 private static Log log = LogFactory.getLog(DateUtil.class); 36 private static String defaultDatePattern = null; 37 private static String timePattern = "HH:mm"; 38 39 41 45 public static String getDatePattern() { 46 Locale locale = LocaleContextHolder.getLocale(); 47 try { 48 defaultDatePattern = ResourceBundle.getBundle(Constants.BUNDLE_KEY, locale) 49 .getString("date.format"); 50 } catch (MissingResourceException mse) { 51 defaultDatePattern = "MM/dd/yyyy"; 52 } 53 54 return defaultDatePattern; 55 } 56 57 public static String getDateTimePattern() { 58 return DateUtil.getDatePattern() + " HH:mm:ss.S"; 59 } 60 61 68 public static final String getDate(Date aDate) { 69 SimpleDateFormat df = null; 70 String returnValue = ""; 71 72 if (aDate != null) { 73 df = new SimpleDateFormat (getDatePattern()); 74 returnValue = df.format(aDate); 75 } 76 77 return (returnValue); 78 } 79 80 90 public static final Date convertStringToDate(String aMask, String strDate) 91 throws ParseException { 92 SimpleDateFormat df = null; 93 Date date = null; 94 df = new SimpleDateFormat (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 pe) { 104 throw new ParseException (pe.getMessage(), pe.getErrorOffset()); 106 } 107 108 return (date); 109 } 110 111 118 public static String getTimeNow(Date theTime) { 119 return getDateTime(timePattern, theTime); 120 } 121 122 128 public static Calendar getToday() throws ParseException { 129 Date today = new Date (); 130 SimpleDateFormat df = new SimpleDateFormat (getDatePattern()); 131 132 String todayAsString = df.format(today); 135 Calendar cal = new GregorianCalendar (); 136 cal.setTime(convertStringToDate(todayAsString)); 137 138 return cal; 139 } 140 141 151 public static final String getDateTime(String aMask, Date aDate) { 152 SimpleDateFormat df = null; 153 String returnValue = ""; 154 155 if (aDate == null) { 156 log.error("aDate is null!"); 157 } else { 158 df = new SimpleDateFormat (aMask); 159 returnValue = df.format(aDate); 160 } 161 162 return (returnValue); 163 } 164 165 173 public static final String convertDateToString(Date aDate) { 174 return getDateTime(getDatePattern(), aDate); 175 } 176 177 185 public static Date convertStringToDate(String strDate) 186 throws ParseException { 187 Date 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 pe) { 196 log.error("Could not convert '" + strDate 197 + "' to a date, throwing exception"); 198 pe.printStackTrace(); 199 throw new ParseException (pe.getMessage(), 200 pe.getErrorOffset()); 201 202 } 203 204 return aDate; 205 } 206 } 207 | Popular Tags |