The following example shows how you might create a tag converter that converts minutes into a formatted string that shows the number of hours and minutes. For example, given installation of this tag converter, a JSP can include the following tag:
<dsp:valueof param="numMinutes"
converter="minutestToHoursMinutes"
converterattributes="asHoursAndMinutes=true"/>
If the page parameter numMinutes
has a value of 117, this tag converter displays the value as follows:
1 hr 57 mins
import atg.droplet.TagConverter; import atg.droplet.TagAttributeDescriptor; import atg.droplet.TagConversionException; import atg.servlet.DynamoHttpServletRequest; import java.util.Properties; //Convert minutes into the format H hr(s) M min(s) public class MinutesToHoursMinutesConverter implements TagConverter { private static final int MINUTES_PER_HOUR = 60; static final String ASHOURSANDMINUTES_ATTRIBUTE = "asHoursAndMinutes"; private static final TagAttributeDescriptor[] S_TAG_ATTRIBUTE_DESCRIPTORS ={ new TagAttributeDescriptor( "ASHOURSANDMINUTES_ATTRIBUTE", "If provided, assume input is # of minutes, format as H hr(s) M min(s)", false, true)}; public String getName() { return "minutesToHoursMinutes"; } public TagAttributeDescriptor[] getTagAttributeDescriptors() { return S_TAG_ATTRIBUTE_DESCRIPTORS; } public Object convertStringToObject( DynamoHttpServletRequest request,String string, Properties properties) throws TagConversionException { throw new TagConversionException("Unable to convert string to object."); } public String convertObjectToString( DynamoHttpServletRequest request, Object pValue, Properties properties) throws TagConversionException { if(pValue == null){ return null; } if (pValue instanceof Integer) { return buildOutput(((Integer)pValue).intValue()); } else { return pValue.toString(); } } private String buildOutput(int totalMinutes) { int hours = (int) Math.floor(totalMinutes / MINUTES_PER_HOUR); int minutes = totalMinutes % MINUTES_PER_HOUR; return formatOutput(hours, minutes); } private String formatOutput(int hours, int minutes) { StringBuffer buf = new StringBuffer(100); if(hours > 0) { buf.append(hours); if(hours == 1) { buf.append(" hr"); } else { buf.append(" hrs"); } } if(minutes > 0) { buf.append(" ").append(minutes); if(minutes == 1){ buf.append(" min"); } else { buf.append(" mins"); } } return buf.toString(); } }