| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.common.icu; |
| 21 | |
|
| 22 | |
import java.lang.reflect.InvocationTargetException; |
| 23 | |
import java.text.DateFormat; |
| 24 | |
import java.text.SimpleDateFormat; |
| 25 | |
import java.util.Date; |
| 26 | |
|
| 27 | |
import org.crosswire.common.util.ClassUtil; |
| 28 | |
import org.crosswire.common.util.ReflectionUtil; |
| 29 | |
import org.slf4j.Logger; |
| 30 | |
import org.slf4j.LoggerFactory; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 0 | public final class DateFormatter { |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public static final int FULL = 0; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public static final int LONG = 1; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public static final int MEDIUM = 2; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public static final int SHORT = 3; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public static final int DEFAULT = MEDIUM; |
| 63 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatter.class); |
| 64 | |
private static final String DEFAULT_SIMPLE_DATE_FORMAT_CLASS = "com.ibm.icu.text.SimpleDateFormat"; |
| 65 | |
private static final String DEFAULT_DATE_FORMAT_CLASS = "com.ibm.icu.text.DateFormat"; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private Object formatter; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
private Class<?> formatterClass; |
| 75 | |
private static Class<?> defaultSimpleDateFormat; |
| 76 | |
private static Class<?> defaultDateFormat; |
| 77 | |
|
| 78 | |
static { |
| 79 | |
try { |
| 80 | 0 | defaultSimpleDateFormat = ClassUtil.forName(DEFAULT_SIMPLE_DATE_FORMAT_CLASS); |
| 81 | 0 | } catch (ClassNotFoundException ex) { |
| 82 | 0 | LOGGER.info("Error loading simple date format class [{}]", DEFAULT_SIMPLE_DATE_FORMAT_CLASS); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
try { |
| 86 | 0 | defaultDateFormat = ClassUtil.forName(DEFAULT_DATE_FORMAT_CLASS); |
| 87 | 0 | } catch (ClassNotFoundException ex) { |
| 88 | 0 | LOGGER.info("Error loading date format class [{}]", DEFAULT_SIMPLE_DATE_FORMAT_CLASS); |
| 89 | 0 | } |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | 0 | private DateFormatter() { |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static DateFormatter getDateInstance(int format) { |
| 106 | 0 | DateFormatter fmt = new DateFormatter(); |
| 107 | 0 | boolean oops = false; |
| 108 | |
try { |
| 109 | 0 | fmt.formatterClass = defaultDateFormat; |
| 110 | |
|
| 111 | |
|
| 112 | 0 | Class<?>[] instanceTypes = { |
| 113 | |
int.class |
| 114 | |
}; |
| 115 | 0 | Object[] instanceParams = { |
| 116 | |
Integer.valueOf(format) |
| 117 | |
}; |
| 118 | 0 | fmt.formatter = ReflectionUtil.invoke(fmt.formatterClass, fmt.formatterClass, "getDateInstance", instanceParams, instanceTypes); |
| 119 | 0 | } catch (NoSuchMethodException e) { |
| 120 | 0 | oops = true; |
| 121 | 0 | } catch (IllegalAccessException e) { |
| 122 | 0 | oops = true; |
| 123 | 0 | } catch (InvocationTargetException e) { |
| 124 | 0 | oops = true; |
| 125 | 0 | } catch (NullPointerException e) { |
| 126 | 0 | oops = true; |
| 127 | 0 | } |
| 128 | |
|
| 129 | 0 | if (oops) { |
| 130 | 0 | fmt.formatterClass = DateFormat.class; |
| 131 | 0 | fmt.formatter = DateFormat.getDateInstance(format); |
| 132 | |
} |
| 133 | |
|
| 134 | 0 | return fmt; |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
public static DateFormatter getDateInstance() { |
| 144 | 0 | return getDateInstance(DEFAULT); |
| 145 | |
} |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public static DateFormatter getSimpleDateInstance(String format) { |
| 155 | 0 | DateFormatter fmt = new DateFormatter(); |
| 156 | 0 | boolean oops = false; |
| 157 | |
try { |
| 158 | 0 | fmt.formatterClass = defaultSimpleDateFormat; |
| 159 | 0 | fmt.formatter = ReflectionUtil.construct(fmt.formatterClass, format); |
| 160 | 0 | } catch (NoSuchMethodException e) { |
| 161 | 0 | oops = true; |
| 162 | 0 | } catch (IllegalAccessException e) { |
| 163 | 0 | oops = true; |
| 164 | 0 | } catch (InvocationTargetException e) { |
| 165 | 0 | oops = true; |
| 166 | 0 | } catch (NullPointerException e) { |
| 167 | 0 | oops = true; |
| 168 | 0 | } catch (InstantiationException e) { |
| 169 | 0 | oops = true; |
| 170 | 0 | } |
| 171 | |
|
| 172 | 0 | if (oops) { |
| 173 | 0 | fmt.formatterClass = SimpleDateFormat.class; |
| 174 | 0 | fmt.formatter = new SimpleDateFormat(format); |
| 175 | |
} |
| 176 | |
|
| 177 | 0 | return fmt; |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public void setLenient(boolean lenient) { |
| 187 | |
try { |
| 188 | 0 | Class<?>[] lenientTypes = { |
| 189 | |
boolean.class |
| 190 | |
}; |
| 191 | 0 | Object[] lenientParams = { |
| 192 | |
Boolean.valueOf(lenient) |
| 193 | |
}; |
| 194 | 0 | ReflectionUtil.invoke(formatterClass, formatter, "setLenient", lenientParams, lenientTypes); |
| 195 | 0 | } catch (NoSuchMethodException e) { |
| 196 | 0 | assert false : e; |
| 197 | 0 | } catch (IllegalAccessException e) { |
| 198 | 0 | assert false : e; |
| 199 | 0 | } catch (InvocationTargetException e) { |
| 200 | 0 | assert false : e; |
| 201 | 0 | } |
| 202 | 0 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public String format(Date date) { |
| 212 | |
try { |
| 213 | 0 | return (String) ReflectionUtil.invoke(formatterClass, formatter, "format", date); |
| 214 | 0 | } catch (NoSuchMethodException e) { |
| 215 | 0 | assert false : e; |
| 216 | 0 | } catch (IllegalAccessException e) { |
| 217 | 0 | assert false : e; |
| 218 | 0 | } catch (InvocationTargetException e) { |
| 219 | 0 | assert false : e; |
| 220 | 0 | } |
| 221 | 0 | return ""; |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public Date parse(String text) { |
| 232 | |
try { |
| 233 | 0 | return (Date) ReflectionUtil.invoke(formatterClass, formatter, "parse", text); |
| 234 | 0 | } catch (NoSuchMethodException e) { |
| 235 | 0 | assert false : e; |
| 236 | 0 | } catch (IllegalAccessException e) { |
| 237 | 0 | assert false : e; |
| 238 | 0 | } catch (InvocationTargetException e) { |
| 239 | 0 | assert false : e; |
| 240 | 0 | } |
| 241 | 0 | return new Date(); |
| 242 | |
} |
| 243 | |
} |