Package net.sourceforge.javautil.common.coersion.impl

Source Code of net.sourceforge.javautil.common.coersion.impl.CoersionSQLDate

package net.sourceforge.javautil.common.coersion.impl;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.sourceforge.javautil.common.coersion.CoersionException;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;

/**
* Standard conversion for {@link Date}'s.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: CoersionSQLDate.java 1536 2009-12-03 22:51:08Z ponderator $
*/
public class CoersionSQLDate extends CoersionAbstract<CharSequence, Date> {
 
  public static final String DATE_FORMAT = "MM/dd/yyyy hh:mm:ssa ZZZ";
 
  public SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);

  public CoersionSQLDate() { super(CharSequence.class, Date.class); }

  public Object coerce(Object original, Class target) {
    try {
      if (original instanceof CharSequence) {
        return format.parse( String.valueOf(original) );
      } else if (original instanceof Date) {
        return format.format( (Date) original );
      }
    } catch (ParseException e) {
      throw new CoersionException(e);
    }

    throw new CoersionException("Could not convert: " + original + " to " + target);
  }

}
TOP

Related Classes of net.sourceforge.javautil.common.coersion.impl.CoersionSQLDate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.