| 1 10 package org.mmbase.bridge.util.xml; 11 12 import org.apache.xpath.XPathAPI; 13 import java.util.*; 14 15 import org.mmbase.bridge.*; 16 17 import org.mmbase.util.logging.Logger; 18 import org.mmbase.util.logging.Logging; 19 20 39 public class DateFormat { 40 41 private static final Logger log = Logging.getLoggerInstance(DateFormat.class); 42 43 49 private static TimeZone getTimeZone(String timeZone) { 50 if (timeZone == null || "".equals(timeZone.trim())) { 51 return TimeZone.getDefault(); 52 } else { 53 TimeZone tz = TimeZone.getTimeZone(timeZone); 54 if (!tz.getID().equals(timeZone)) { 55 tz = TimeZone.getDefault(); 56 } 57 return tz; 58 } 59 } 60 61 71 public static String format(String number, String fieldName, String pattern, String timeZone, String language, String country) { 72 return format("mmbase", number, fieldName, pattern, timeZone, language, country); 73 } 74 75 76 87 public static String format(String cloudName, String number, String fieldName, String pattern, String timeZone, String language, String country) { 88 try { 89 Cloud cloud = LocalContext.getCloudContext().getCloud(cloudName); 90 cloud.setLocale(new Locale(language, country)); 91 return format(cloud, number, fieldName, pattern, timeZone); 92 } catch (BridgeException e) { 93 return "could not find '" + fieldName + "' on node '" + number + "' (" + e.toString() + ")"; 94 } 95 } 96 97 98 107 public static String format(Cloud cloud, String number, String fieldName, String pattern, String timeZone) { 108 try { 109 Node node = cloud.getNode(number); 110 String fieldValue = "" + node.getIntValue(fieldName); 111 return format(fieldValue, pattern, 1000, timeZone, cloud.getLocale()); 112 } catch (BridgeException e) { 113 if (log.isDebugEnabled()) { 114 log.debug("could not find '" + fieldName + "' on node '" + number + "'"); 115 log.trace(Logging.stackTrace(e)); 116 } 117 return "could not find " + fieldName + " on node " + number + "(" + e.toString() + ")"; 118 } 119 } 120 121 131 public static String format(String fieldValue, String pattern, String timeZone, String language, String country) { 132 return format(fieldValue, pattern, 1000, timeZone, language, country); 133 } 134 135 145 public static String format(String fieldValue, String pattern, int factor, String timeZone, String language, String country) { 146 return format(fieldValue, pattern, factor, timeZone, new Locale(language, country)); 147 } 148 149 157 protected static String format(String fieldValue, String pattern, int factor, String timeZone, Locale locale) { 158 if (fieldValue == null || "".equals(fieldValue)) { 159 return ""; 160 } 161 java.text.DateFormat df = org.mmbase.util.DateFormats.getInstance(pattern, timeZone, locale); 162 long seconds = Long.valueOf(fieldValue).longValue(); 163 return df.format(new Date(seconds * factor)); 164 } 165 166 179 public static String format(Cloud cloud, org.w3c.dom.Node node, String fieldName, String pattern, String timeZone) throws javax.xml.transform.TransformerException { 180 log.debug("calling with dom node"); 181 String number = XPathAPI.eval(node, "./field[@name='number']").toString(); 183 return format(cloud, number, fieldName, pattern, timeZone); 184 } 185 186 191 public static int getYear(String fieldValue) { 192 return getDatePart(fieldValue, 1000, Calendar.YEAR, ""); 193 } 194 195 200 public static int getMonth(String fieldValue) { 201 return getDatePart(fieldValue, 1000, Calendar.MONTH, "") + 1; 202 } 203 204 209 public static int getDay(String fieldValue) { 210 return getDatePart(fieldValue, 1000, Calendar.DAY_OF_MONTH, ""); 211 } 212 213 218 public static int getHours(String fieldValue) { 219 return getDatePart(fieldValue, 1000, Calendar.HOUR_OF_DAY, ""); 220 } 221 222 227 public static int getMinutes(String fieldValue) { 228 return getDatePart(fieldValue, 1000, Calendar.MINUTE, ""); 229 } 230 231 236 public static int getSeconds(String fieldValue) { 237 return getDatePart(fieldValue, 1000, Calendar.SECOND, ""); 238 } 239 240 246 public static int getYear(String fieldValue, String timeZone) { 247 return getDatePart(fieldValue, 1000, Calendar.YEAR, timeZone); 248 } 249 250 256 public static int getMonth(String fieldValue, String timeZone) { 257 return getDatePart(fieldValue, 1000, Calendar.MONTH, timeZone) + 1; 258 } 259 260 266 public static int getDay(String fieldValue, String timeZone) { 267 return getDatePart(fieldValue, 1000, Calendar.DAY_OF_MONTH, timeZone); 268 } 269 270 276 public static int getHours(String fieldValue, String timeZone) { 277 return getDatePart(fieldValue, 1000, Calendar.HOUR_OF_DAY, timeZone); 278 } 279 280 286 public static int getMinutes(String fieldValue, String timeZone) { 287 return getDatePart(fieldValue, 1000, Calendar.MINUTE, timeZone); 288 } 289 290 296 public static int getSeconds(String fieldValue, String timeZone) { 297 return getDatePart(fieldValue, 1000, Calendar.SECOND, timeZone); 298 } 299 300 307 public static int getDatePart(String fieldValue, int factor, int datePart) { 308 return getDatePart(fieldValue, factor, datePart, ""); 309 } 310 311 319 public static int getDatePart(String fieldValue, int factor, int datePart, String timeZone) { 320 if (fieldValue != null && !"".equals(fieldValue.trim())) { 321 Calendar cal = Calendar.getInstance(getTimeZone(timeZone)); 322 long seconds = Long.valueOf(fieldValue).longValue(); 323 cal.setTimeInMillis(seconds * factor); 324 return cal.get(datePart); 325 } else { 326 return -1; 327 } 328 } 329 330 } 331 | Popular Tags |