All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jpaoletti.jpm.struts.converter.EditDateConverter Maven / Gradle / Ivy

The newest version!
package jpaoletti.jpm.struts.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import jpaoletti.jpm.converter.ConverterException;
import jpaoletti.jpm.core.PMContext;
import jpaoletti.jpm.core.message.MessageFactory;

/**Converter for date.
*
 * {@code
 * 
 *     edit (or add)
 *     
 *         
 *     
 * 
 * }
 * 
* @author jpaoletti * */ public class EditDateConverter extends EditStringConverter { @Override public Object build(PMContext ctx) throws ConverterException { try { String value = (String) ctx.getFieldValue(); if (value != null && !"".equals(value.trim())) { return getDateFormat().parse((String) value); } } catch (ParseException e) { throw new ConverterException(MessageFactory.error( ctx.getEntity(), ctx.getField(), "date.converter.invalid.value")); } return null; } @Override public String visualize(PMContext ctx) throws ConverterException { try { final Date o = (Date) ctx.getFieldValue(); return super.visualize("date_converter.jsp?format=" + normalize(javaToJavascriptDateFormat(getFormatString())) + "&value=" + getDateFormat().format(o)); } catch (Exception e) { return super.visualize("date_converter.jsp?format=" + normalize(javaToJavascriptDateFormat(getFormatString())) + "&value="); } } /** * Return the format object of the date * @return The format */ public DateFormat getDateFormat() { DateFormat df = new SimpleDateFormat(getFormatString()); return df; } private String getFormatString() { return getConfig("format", "MM/dd/yyyy"); } private String javaToJavascriptDateFormat(String s) { /* * The format can be combinations of the following: * d - day of month (no leading zero) * dd - day of month (two digit) * D - day name short * DD - day name long * m - month of year (no leading zero) * mm - month of year (two digit) * M - month name short * MM - month name long * y - year (two digit) * yy - year (four digit) */ return s.replaceAll("yy", "y").replace('M', 'm'); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy