| 1 |
/* |
| 2 |
* @(#) DateConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import java.text.DateFormat; |
| 9 |
import java.text.ParseException; |
| 10 |
import java.text.SimpleDateFormat; |
| 11 |
import java.util.Calendar; |
| 12 |
import java.util.Date; |
| 13 |
import java.util.TimeZone; |
| 14 |
|
| 15 |
/** |
| 16 |
* Converter which converts Date to String and converts it back. |
| 17 |
*/ |
| 18 |
public class DateConverter implements ObjectConverter { |
| 19 |
|
| 20 |
public static final ConverterContext DATETIME_CONTEXT = new ConverterContext("DateTime"); |
| 21 |
public static final ConverterContext TIME_CONTEXT = new ConverterContext("Time"); |
| 22 |
public static final ConverterContext DATE_CONTEXT = new ConverterContext("Date"); |
| 23 |
|
| 24 |
private DateFormat _shortFormat = SimpleDateFormat.getDateInstance(DateFormat.SHORT); |
| 25 |
private DateFormat _mediumFormat = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM); |
| 26 |
private DateFormat _longFormat = SimpleDateFormat.getDateInstance(DateFormat.LONG); |
| 27 |
|
| 28 |
private DateFormat _defaultFormat = SimpleDateFormat.getDateInstance(DateFormat.DEFAULT); |
| 29 |
|
| 30 |
private DateFormat _shortDatetimeFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); |
| 31 |
private DateFormat _mediumDatetimeFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); |
| 32 |
private DateFormat _longDatetimeFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); |
| 33 |
|
| 34 |
private DateFormat _defaultDatetimeFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT); |
| 35 |
|
| 36 |
private DateFormat _shortTimeFormat = SimpleDateFormat.getTimeInstance(DateFormat.SHORT); |
| 37 |
private DateFormat _mediumTimeFormat = SimpleDateFormat.getTimeInstance(DateFormat.MEDIUM); |
| 38 |
private DateFormat _longTimeFormat = SimpleDateFormat.getTimeInstance(DateFormat.LONG); |
| 39 |
|
| 40 |
private DateFormat _defaultTimeFormat = SimpleDateFormat.getTimeInstance(DateFormat.DEFAULT); |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Creates a DateConverter. |
| 45 |
*/ |
| 46 |
public DateConverter() { |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Converts the object to String. The object can be a Calendar, a Date or a Number. As long as the DateFormat can |
| 51 |
* format it correctly, it will be converted to a String. If the object is already a String, we will return it |
| 52 |
* directly as it is. |
| 53 |
* |
| 54 |
* @param object the object to be converted |
| 55 |
* @param context the converter context. |
| 56 |
* @return the string |
| 57 |
*/ |
| 58 |
synchronized public String toString(Object object, ConverterContext context) { |
| 59 |
if (object == null) { |
| 60 |
return ""; |
| 61 |
} |
| 62 |
else { |
| 63 |
TimeZone timeZone; |
| 64 |
if (object instanceof Calendar) { |
| 65 |
timeZone = ((Calendar) object).getTimeZone(); |
| 66 |
object = ((Calendar) object).getTime(); |
| 67 |
} |
| 68 |
else if (object instanceof Date) { |
| 69 |
Calendar cal = Calendar.getInstance(); |
| 70 |
cal.setTime(((Date) object)); |
| 71 |
timeZone = cal.getTimeZone(); |
| 72 |
} |
| 73 |
else { |
| 74 |
timeZone = TimeZone.getDefault(); |
| 75 |
} |
| 76 |
|
| 77 |
if (object instanceof Date || object instanceof Number) { |
| 78 |
if (context != null && context.getUserObject() instanceof DateFormat) { |
| 79 |
return ((DateFormat) context.getUserObject()).format(object); |
| 80 |
} |
| 81 |
else if (DATETIME_CONTEXT.equals(context)) { |
| 82 |
_defaultDatetimeFormat.setTimeZone(timeZone); |
| 83 |
return _defaultDatetimeFormat.format(object); |
| 84 |
} |
| 85 |
else if (TIME_CONTEXT.equals(context)) { |
| 86 |
_defaultTimeFormat.setTimeZone(timeZone); |
| 87 |
return _defaultTimeFormat.format(object); |
| 88 |
} |
| 89 |
else { |
| 90 |
_defaultFormat.setTimeZone(timeZone); |
| 91 |
return _defaultFormat.format(object); |
| 92 |
} |
| 93 |
} |
| 94 |
else if (object instanceof String) { |
| 95 |
return (String) object; |
| 96 |
} |
| 97 |
else { |
| 98 |
return null; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public boolean supportToString(Object object, ConverterContext context) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Converts from a String to a Date. |
| 109 |
* |
| 110 |
* @param string the string to be converted. |
| 111 |
* @param context the context. It could be DATETIME_CONTEXT, DATE_CONTEXT or TIME_CONTEXT. |
| 112 |
* @return the Date. If the string is null or empty, null will be returned. If the string cannot be parsed as a |
| 113 |
* date, the string itself will be returned. |
| 114 |
*/ |
| 115 |
synchronized public Object fromString(String string, ConverterContext context) { |
| 116 |
if (string == null) { |
| 117 |
return null; |
| 118 |
} |
| 119 |
string = string.trim(); |
| 120 |
if (string.length() == 0) { |
| 121 |
return null; |
| 122 |
} |
| 123 |
|
| 124 |
try { |
| 125 |
Object userObject = context != null ? context.getUserObject() : null; |
| 126 |
if (userObject instanceof DateFormat) { |
| 127 |
return ((DateFormat) userObject).parse(string); |
| 128 |
} |
| 129 |
else if (DATETIME_CONTEXT.equals(context)) { |
| 130 |
return _defaultDatetimeFormat.parse(string); |
| 131 |
} |
| 132 |
else if (TIME_CONTEXT.equals(context)) { |
| 133 |
return _defaultTimeFormat.parse(string); |
| 134 |
} |
| 135 |
else { |
| 136 |
return _defaultFormat.parse(string); |
| 137 |
} |
| 138 |
} |
| 139 |
catch (ParseException e1) { // if current formatter doesn't work try those default ones. |
| 140 |
if (DATETIME_CONTEXT.equals(context)) { |
| 141 |
try { |
| 142 |
return _shortDatetimeFormat.parse(string); |
| 143 |
} |
| 144 |
catch (ParseException e2) { |
| 145 |
try { |
| 146 |
return _mediumDatetimeFormat.parse(string); |
| 147 |
} |
| 148 |
catch (ParseException e3) { |
| 149 |
try { |
| 150 |
return _longDatetimeFormat.parse(string); |
| 151 |
} |
| 152 |
catch (ParseException e4) { |
| 153 |
// null |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
else if (TIME_CONTEXT.equals(context)) { |
| 159 |
try { |
| 160 |
return _shortTimeFormat.parse(string); |
| 161 |
} |
| 162 |
catch (ParseException e2) { |
| 163 |
try { |
| 164 |
return _mediumTimeFormat.parse(string); |
| 165 |
} |
| 166 |
catch (ParseException e3) { |
| 167 |
try { |
| 168 |
return _longTimeFormat.parse(string); |
| 169 |
} |
| 170 |
catch (ParseException e4) { |
| 171 |
return string; // nothing works just return null so that old value will be kept. |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
else { |
| 177 |
try { |
| 178 |
return _shortFormat.parse(string); |
| 179 |
} |
| 180 |
catch (ParseException e2) { |
| 181 |
try { |
| 182 |
return _mediumFormat.parse(string); |
| 183 |
} |
| 184 |
catch (ParseException e3) { |
| 185 |
try { |
| 186 |
return _longFormat.parse(string); |
| 187 |
} |
| 188 |
catch (ParseException e4) { |
| 189 |
// null |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// try other default formats |
| 197 |
String[] formatStrings = {"yyyy-mm-dd", "yy-mm-dd", "yyyymmdd", "yymmdd", "dd-MMM-yy", "dd-MMM-yyyy"}; |
| 198 |
SimpleDateFormat sdf; |
| 199 |
for (String formatString : formatStrings) { |
| 200 |
try { |
| 201 |
sdf = new SimpleDateFormat(formatString); |
| 202 |
return sdf.parse(string); |
| 203 |
} |
| 204 |
catch (ParseException ex) { |
| 205 |
// break; |
| 206 |
} |
| 207 |
} |
| 208 |
return string; // nothing works just return null so that old value will be kept. |
| 209 |
} |
| 210 |
|
| 211 |
public boolean supportFromString(String string, ConverterContext context) { |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Gets DefaultFormat to format an calendar. |
| 217 |
* |
| 218 |
* @return DefaultFormat |
| 219 |
*/ |
| 220 |
public DateFormat getDefaultFormat() { |
| 221 |
return _defaultFormat; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Sets DefaultFormat to format an calendar. |
| 226 |
* |
| 227 |
* @param defaultFormat the new default date format |
| 228 |
*/ |
| 229 |
public void setDefaultFormat(DateFormat defaultFormat) { |
| 230 |
_defaultFormat = defaultFormat; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Gets DefaultTimeFormat to format an calendar. This is used only when context is {@link #TIME_CONTEXT}. |
| 235 |
* |
| 236 |
* @return DefaultTimeFormat |
| 237 |
*/ |
| 238 |
public DateFormat getDefaultTimeFormat() { |
| 239 |
return _defaultTimeFormat; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Sets DefaultTimeFormat to format an calendar. This is used only when context is {@link #TIME_CONTEXT}. |
| 244 |
* |
| 245 |
* @param defaultTimeFormat the new default time format |
| 246 |
*/ |
| 247 |
public void setDefaultTimeFormat(DateFormat defaultTimeFormat) { |
| 248 |
_defaultTimeFormat = defaultTimeFormat; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Gets DefaultDatetimeFormat to format an calendar. This is used only when context is {@link #DATETIME_CONTEXT}. |
| 253 |
* |
| 254 |
* @return DefaultDatetimeFormat |
| 255 |
*/ |
| 256 |
public DateFormat getDefaultDatetimeFormat() { |
| 257 |
return _defaultDatetimeFormat; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Sets DefaultDatetimeFormat to format an calendar. This is used only when context is {@link #DATETIME_CONTEXT}. |
| 262 |
* |
| 263 |
* @param defaultDatetimeFormat the new defaultdatetime format |
| 264 |
*/ |
| 265 |
public void setDefaultDatetimeFormat(DateFormat defaultDatetimeFormat) { |
| 266 |
_defaultDatetimeFormat = defaultDatetimeFormat; |
| 267 |
} |
| 268 |
} |