Date Converter

Java method in myUtils to convert dates:
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	public static String getDateFormatted(Date passedDate) {
		return DATE_FORMAT.format(passedDate);
	}

Registration of converter in faces-config:
  <converter>
    <converter-id>dateConverter</converter-id>
    <converter-class>converter.DateConverter</converter-class>
  </converter>

Java converter class:
package converter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import uk.co.intec.util.IntecUtils;

public class DateConverter implements Converter {
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

	/* (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	 */
	public Object getAsObject(final FacesContext context, final UIComponent component, final String value) {
		return value;
	}

	/* (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	public String getAsString(final FacesContext context, final UIComponent component, final Object value) {
		if (value instanceof Iterable) {
			StringBuilder result = new StringBuilder();
			boolean appended = false;
			Iterable<?> values = (Iterable<?>) value;

			// Get the separator - a "-" if two values, a "," if more
			String separator = "";
			int count = 0;
			Iterator it = values.iterator();
			while (it.hasNext()) {
				Object nextDate = it.next();
				if (count > 1) {
					separator = ", ";
					break;
				} else if (count > 0) {
					separator = "-";
				}
				count++;
			}
			for (Object node : values) {
				if (appended) {
					result.append(separator);
				} else {
					appended = true;
				}
				result.append(convertDate(node));
			}
			return result.toString();
		}
		return convertDate(value);
	}

	/**
	 * @param value
	 * @return
	 */
	public String convertDate(final Object value) {
		if (value instanceof java.util.Date) {
			return myUtils.getDateFormatted((Date) value);
		} else {
			return String.valueOf(value);
		}
	}

}

Code on XPage:
<xp:text id="myDate" value="#{myEnt.myDates}">
	<xp:this.converter>
		<xp:converter converterId="dateConverterDateOnly"></xp:converter>
	</xp:this.converter>
</xp:text>





Converter to convert single or multi-value dates to a standard format.
If a column has a single date, it's converted.
If it is multi-value with two dates, it's output in format date1-date2
If it is multi-value with more than two dates, it's outputted in format date1, date2, date3 etc

Java
Paul S Withers
May 1, 2014 7:40 AM
Rating
13

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...