Announcement Announcement Module
Collapse
No announcement yet.
creating custom date formatter in spring framework Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • creating custom date formatter in spring framework

    i want to define custom date formatter for my application. in ApplicationContext.xml i have defined it as


    HTML Code:
       <bean id="conversionService" class="org.springframework.format.datetime">
         <property name ="converters">
             <set>
                <bean class="com.ipc.ds.ws.marshaller.DateFormatter"/>
             </set>
         </property>
      </bean>
    and i have defined DateFormatter as below,

    HTML Code:
    package com.ipc.ds.ws.marshaller;
    
    import java.text.ParseException ;
    import java.text.SimpleDateFormat ;
    import java.util.Date ;
    import java.util.Locale ;
    import org.springframework.format.Formatter ;
    
    public class DateFormatter implements Formatter<Date>
    
    {
    	DateFormatter(String source) throws ParseException 
    	{
    		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss") ;
    		Date date = null;
    		date = sdf.parse(source);
    	}
    
    	/* (non-Javadoc)
    	 * @see org.springframework.format.Printer#print(java.lang.Object, java.util.Locale)
    	 */
    	@Override
    	public String print(Date object, Locale locale)
    	{
    		// TODO Auto-generated method stub
    		return null ;
    	}
    
    	/* (non-Javadoc)
    	 * @see org.springframework.format.Parser#parse(java.lang.String, java.util.Locale)
    	 */
    	@Override
    	public Date parse(String source, Locale locale) throws ParseException
    	{
    		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss") ;
    		Date date = null;
    		if(source!=null && !source.trim().equals(""))
    		{
    			try
    			{
    				date = sdf.parse(source);
    			}
    			catch (ParseException e)
    			{
    				throw new IllegalArgumentException("Invalid date format passed as argument value");
    			}
    		}
    		else
    		{
    			throw new IllegalArgumentException("Invalid date passed as argument value");
    		}
    		return date;
    	}
    
    }

    Am i doing it right here?
Working...
X