1 16 package org.apache.log4j.lf5.util; 17 18 import java.text.DateFormat ; 19 import java.text.ParseException ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Date ; 22 import java.util.Locale ; 23 import java.util.TimeZone ; 24 25 35 36 public class DateFormatManager { 38 42 46 private TimeZone _timeZone = null; 50 private Locale _locale = null; 51 52 private String _pattern = null; 53 private DateFormat _dateFormat = null; 54 55 public DateFormatManager() { 59 super(); 60 configure(); 61 } 62 63 public DateFormatManager(TimeZone timeZone) { 64 super(); 65 66 _timeZone = timeZone; 67 configure(); 68 } 69 70 public DateFormatManager(Locale locale) { 71 super(); 72 73 _locale = locale; 74 configure(); 75 } 76 77 public DateFormatManager(String pattern) { 78 super(); 79 80 _pattern = pattern; 81 configure(); 82 } 83 84 public DateFormatManager(TimeZone timeZone, Locale locale) { 85 super(); 86 87 _timeZone = timeZone; 88 _locale = locale; 89 configure(); 90 } 91 92 public DateFormatManager(TimeZone timeZone, String pattern) { 93 super(); 94 95 _timeZone = timeZone; 96 _pattern = pattern; 97 configure(); 98 } 99 100 public DateFormatManager(Locale locale, String pattern) { 101 super(); 102 103 _locale = locale; 104 _pattern = pattern; 105 configure(); 106 } 107 108 public DateFormatManager(TimeZone timeZone, Locale locale, String pattern) { 109 super(); 110 111 _timeZone = timeZone; 112 _locale = locale; 113 _pattern = pattern; 114 configure(); 115 } 116 117 121 public synchronized TimeZone getTimeZone() { 122 if (_timeZone == null) { 123 return TimeZone.getDefault(); 124 } else { 125 return _timeZone; 126 } 127 } 128 129 public synchronized void setTimeZone(TimeZone timeZone) { 130 timeZone = timeZone; 131 configure(); 132 } 133 134 public synchronized Locale getLocale() { 135 if (_locale == null) { 136 return Locale.getDefault(); 137 } else { 138 return _locale; 139 } 140 } 141 142 public synchronized void setLocale(Locale locale) { 143 _locale = locale; 144 configure(); 145 } 146 147 public synchronized String getPattern() { 148 return _pattern; 149 } 150 151 154 public synchronized void setPattern(String pattern) { 155 _pattern = pattern; 156 configure(); 157 } 158 159 160 164 public synchronized String getOutputFormat() { 165 return _pattern; 166 } 167 168 172 public synchronized void setOutputFormat(String pattern) { 173 _pattern = pattern; 174 configure(); 175 } 176 177 public synchronized DateFormat getDateFormatInstance() { 178 return _dateFormat; 179 } 180 181 public synchronized void setDateFormatInstance(DateFormat dateFormat) { 182 _dateFormat = dateFormat; 183 } 185 186 public String format(Date date) { 187 return getDateFormatInstance().format(date); 188 } 189 190 public String format(Date date, String pattern) { 191 DateFormat formatter = null; 192 formatter = getDateFormatInstance(); 193 if (formatter instanceof SimpleDateFormat ) { 194 formatter = (SimpleDateFormat ) (formatter.clone()); 195 ((SimpleDateFormat ) formatter).applyPattern(pattern); 196 } 197 return formatter.format(date); 198 } 199 200 203 public Date parse(String date) throws ParseException { 204 return getDateFormatInstance().parse(date); 205 } 206 207 210 public Date parse(String date, String pattern) throws ParseException { 211 DateFormat formatter = null; 212 formatter = getDateFormatInstance(); 213 if (formatter instanceof SimpleDateFormat ) { 214 formatter = (SimpleDateFormat ) (formatter.clone()); 215 ((SimpleDateFormat ) formatter).applyPattern(pattern); 216 } 217 return formatter.parse(date); 218 } 219 220 224 private synchronized void configure() { 228 _dateFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.FULL, 229 DateFormat.FULL, 230 getLocale()); 231 _dateFormat.setTimeZone(getTimeZone()); 232 233 if (_pattern != null) { 234 ((SimpleDateFormat ) _dateFormat).applyPattern(_pattern); 235 } 236 } 237 238 242 } 243 | Popular Tags |