⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datetransform.java

📁 数据仓库工具
💻 JAVA
字号:
/*
	Copyright (C) 2003  Together

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.webdocwf.util.loader.transformation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

/**
 * This class implements Transformer interface. It can be used to transform date from one
 * format to another
 * Date: 22.07.2004.
 * @version 1.0
 * @author Zeljko Kovacevic
 *
 */
public class DateTransform implements Transformer {

    private List retValue = new Vector();
    private String dateFormat = "";
		private String filePath = "";
		private Properties properties = new Properties();
    /** This method uses to set configuration string
     * which contains pattern for new date format
     * @param filePath to property file
     */
    public void configure(String filePath) throws Exception {
        
		try {

				   this.filePath = filePath;
				   File propertyFile = new File(this.filePath);
				   this.properties.load(new FileInputStream(propertyFile));
				   this.dateFormat = this.properties.getProperty("dateFormat");

			   } catch (FileNotFoundException e) {
				   throw new Exception("File not found!");
			   } catch (IOException e) {
				   throw new Exception("Error while reading property file!");
			   }
    }

    /** 
     * This method release resources
     */
    public void release() throws Exception {

    }

    /** This method transform date format
     * @param valueToTransform List for transformation
     * @return List with transformed values
     */
    public List transformValue(List valueToTransform) throws Exception {
        retValue.clear();
        try {
            if (this.dateFormat == "") {
                //if not set new date format DO NOTHING
                this.retValue = valueToTransform;
            } else {
                SimpleDateFormat formatDate = new SimpleDateFormat(this.dateFormat);
                String strDate = valueToTransform.get(0).toString();
                Time tmpTime = null;
                Date tmpDate = null;
                Timestamp tmpTimestamp = null;
                String readValue = null;
                try {
                    //if input data is Date type
                    tmpDate = Date.valueOf(strDate);
                } catch (Exception e) {
                    try {
                        //if input data is Time type
                        tmpTime = Time.valueOf(strDate);
                    } catch (Exception e1) {
                        try {
                            //if input data is Timestamp type
                            tmpTimestamp = Timestamp.valueOf(strDate);
                        } catch (Exception e2) {
                            throw new Exception("Error. Input date format is not supported!");
                        }
                    }
                }
                if (tmpTime != null) {
                    readValue = formatDate.format(tmpTime);
                } else if (tmpDate != null) {
                    readValue = formatDate.format(tmpDate);
                } else if (tmpTimestamp != null) {
                    readValue = formatDate.format(tmpTimestamp);
                }

                retValue.add(readValue);
            }
        } catch (Exception e) {
            throw new Exception("Error while transform format of date using class DateTransform!",e);
        }
        return retValue;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -