Package com.alibaba.toolkit.util.typeconvert.converters

Source Code of com.alibaba.toolkit.util.typeconvert.converters.SqlDateConverter

/*
* Copyright 2010 Alibaba Group Holding Limited.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.alibaba.toolkit.util.typeconvert.converters;

import com.alibaba.toolkit.util.typeconvert.ConvertChain;
import com.alibaba.toolkit.util.typeconvert.ConvertFailedException;
import com.alibaba.toolkit.util.typeconvert.Converter;

import java.sql.Date;

/**
* ������ת����<code>java.sql.Date</code>.
*
* <ul>
* <li>
* �������Ϊ<code>null</code>, ���׳���Ĭ��ֵ��<code>ConvertFailedException</code>.
* </li>
* <li>
* ��������Ѿ���<code>java.sql.Date</code>��, ֱ�ӷ���.
* </li>
* <li>
* ��������ǿ��ַ���, ���׳���Ĭ��ֵ��<code>ConvertFailedException</code>.
* </li>
* <li>
* ����������ַ���, �����Ű���ת����<code>java.sql.Date</code>, ��ʽ����Ϊ"yyyy-mm-dd".  ������ɹ�,
* ���׳�<code>ConvertFailedException</code>.
* </li>
* <li>
* ����, �Ѷ��󴫵ݸ���һ��<code>Converter</code>����.
* </li>
* </ul>
*
*
* @version $Id: SqlDateConverter.java,v 1.1 2003/07/03 07:26:41 baobao Exp $
* @author Michael Zhou
*/
public class SqlDateConverter implements Converter {
    protected static final Date DEFAULT_VALUE = null;

    public Object convert(Object value, ConvertChain chain) {
        if (value == null) {
            throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
        }

        if (value instanceof Date) {
            return value;
        }

        if (value instanceof String) {
            String strValue = ((String) value).trim();

            try {
                return Date.valueOf(strValue);
            } catch (IllegalArgumentException e) {
                if (strValue.length() > 0) {
                    throw new ConvertFailedException(e);
                }

                throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
            }
        }

        return chain.convert(value);
    }
}
TOP

Related Classes of com.alibaba.toolkit.util.typeconvert.converters.SqlDateConverter

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.