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

📄 timestampconverter.java

📁 struts+hibernate开发的BBS源码
💻 JAVA
字号:
package com.bbs.util;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.beanutils.ConversionException;import org.apache.commons.lang.StringUtils;/** * This class is converts a java.util.Date to a String and a String to a * java.util.Date for use as a Timestamp. It is used by BeanUtils when copying * properties. *  * <p> * <a href="TimestampConverter.java.html"><i>View Source</i></a> * </p> *  * @author <a href="mailto:dan@getrolling.com">Dan Kibler</a> */public class TimestampConverter extends DateConverter {    public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S";    protected Object convertToDate(Class type, Object value) {        DateFormat df = new SimpleDateFormat(TS_FORMAT);        if (value instanceof String) {            try {                if (StringUtils.isEmpty(value.toString())) {                    return null;                }                return df.parse((String) value);            } catch (Exception pe) {                throw new ConversionException("Error converting String to Timestamp");            }        }        throw new ConversionException("Could not convert "                + value.getClass().getName() + " to " + type.getName());    }    protected Object convertToString(Class type, Object value) {        DateFormat df = new SimpleDateFormat(TS_FORMAT);        if (value instanceof Date) {            try {                return df.format(value);            } catch (Exception e) {                throw new ConversionException("Error converting Timestamp to String");            }        }        return value.toString();    }}

⌨️ 快捷键说明

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