/** * Converts a field to the specified type. Useful when * @param type * @param fieldValue * @return */ public static Object convertField(String type, String fieldValue) { if (fieldValue == null) { return null; } try { if (type.equals(SupportedFieldType.BOOLEAN.toString())) { return Boolean.parseBoolean(fieldValue); } else if (type.equals(SupportedFieldType.DATE.toString())) { return FormatUtil.getTimeZoneFormat().parse(fieldValue); } else if (type.equals(SupportedFieldType.INTEGER.toString())) { return Integer.parseInt(fieldValue); } else if (type.equals(SupportedFieldType.MONEY.toString()) || type.equals(SupportedFieldType.DECIMAL.toString())) { return new BigDecimal(fieldValue); } } catch (ParseException e) { throw new RuntimeException(e); } throw new IllegalArgumentException("Unrecognized type(" + type + ") for map field conversion."); }
/** * Parse the string value of the date stored in mvel * * @param date the mvel date value * @return the parsed Date instance */ public static Date parseDate(String date) throws ParseException { Date parsedDate; try { parsedDate = FormatUtil.getTimeZoneFormat().parse(date); } catch (ParseException e) { try { SimpleDateFormat formatter = new SimpleDateFormat(COMPATIBILITY_FORMAT); formatter.setTimeZone(BroadleafRequestContext.getBroadleafRequestContext().getTimeZone()); parsedDate = formatter.parse(date); } catch (ParseException e1) { try { SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT); formatter.setTimeZone(BroadleafRequestContext.getBroadleafRequestContext().getTimeZone()); parsedDate = formatter.parse(date); } catch (ParseException e2) { throw e; } } } return parsedDate; } }
"and you entered: (" + parsableVal +"). Dates must be in the format MM/dd/yyyy HH:mm."); String convertedDate = FormatUtil.getTimeZoneFormat().format(temp); response.append("MvelHelper.convertField(\"DATE\",\""); response.append(convertedDate);