@SuppressWarnings("rawtypes") public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) { String value = values[0]; if (value.matches("^(\\d+)$")) return new Date(Long.parseLong(value)); try { return _format.parse(value); } catch (ParseException e) { e.printStackTrace(); throw new TypeConversionException(e); } } return null; }
public class StringToDateTimeConverter extends StrutsTypeConverter { // WARNING not safe in multi-threaded environments private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss"); public Object convertFromString(Map context, String[] strings, Class toClass) { if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) { return null; } try { return DATETIME_FORMAT.parse(strings[0]); } catch (ParseException e) { throw new TypeConversionException("Unable to convert given object to date: " + strings[0]); } } public String convertToString(Map context, Object date) { if (date != null && date instanceof Date) { return DATETIME_FORMAT.format(date); } else { return null; } } }
package util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import com.opensymphony.xwork2.conversion.TypeConversionException; import ognl.DefaultTypeConverter; public class MyDateConverter extends DefaultTypeConverter { @Override public Object convertValue(Map context, Object object, Class type) { if (type == Date.class) { String datePattern = "yyyy.MM.dd"; DateFormat format = new SimpleDateFormat(datePattern); format.setLenient(false); try { String[] dateString = (String[]) object; return format.parse(dateString[0]); } catch (Exception e) { throw new TypeConversionException("Given Date is Invalid"); } } return null; } }
@SuppressWarnings("rawtypes") public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) { String value = values[0]; if (value.matches("^(\\d+)$")) return new Date(Long.parseLong(value)); try { return _format.parse(value); } catch (ParseException e) { e.printStackTrace(); throw new TypeConversionException(e); } } return null; }
@Override public Object convertFromString(Map map, String[] strings, Class aClass) { if (strings.length == 1) { String string = strings[0]; string = string.replace(",", "."); Double parsedValue; if (string == null || string.isEmpty()) { if (double.class.equals(aClass)) { parsedValue = 0.; } else { parsedValue = null; } } else { parsedValue = Double.parseDouble(string); } return parsedValue; } else { throw new TypeConversionException("strings=" + Arrays.toString(strings)); } }
public class MyTimeStampConverter extends StrutsTypeConverter { private static final SimpleDateFormat sdf = new SimpleDateFormat("<!--Desired Format here-->"); @Override public Object convertFromString(Map context, String[] values, Class toClass) throws TypeConversionException { try{ if (values == null || values.length == 0) { return null; } return new java.sql.Timestamp(sdf.parse(values[0]).getTime()); }catch(Exception e){ throw new TypeConversionException(e); } } @Override public String convertToString(Map context, Object object) throws TypeConversionException { try{ if(object instanceof Timestamp){ return sdf.format(new Date(((Timestamp)object).getTime())); } return ""; }catch(Exception e){ throw new TypeConversionException(e); } } }
@SuppressWarnings("rawtypes") public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) { String value = values[0]; if (value.matches("^(\\d+)$")) { long v = Long.parseLong(value); if( toClass == Long.TYPE || toClass == Long.class) return v; else return new Date(v); } try { Date d = _format.parse(value); if( toClass == Long.TYPE || toClass == Long.class) return d.getTime(); return d; } catch (ParseException e) { e.printStackTrace(); throw new TypeConversionException(e); } } return null; }
return dateFormat.parse(text); } catch (ParseException ex) { throw new TypeConversionException(ex);
@SuppressWarnings("rawtypes") public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) { String value = values[0]; if (value.matches("^(\\d+)$")) { long v = Long.parseLong(value); if( toClass == Long.TYPE || toClass == Long.class) return v; else return new Date(v); } try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date d = format.parse(value); if( toClass == Long.TYPE || toClass == Long.class) return d.getTime(); return d; } catch (ParseException e) { e.printStackTrace(); throw new TypeConversionException(e); } } return null; }
throw new TypeConversionException("unable to parse " + string, e); } catch (NumberFormatException e) { throw new TypeConversionException("unable to parse " + string, e);