【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“TimestampConverter.java”一文,供大家参考学习
package com.welshow.hbp.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:3710167@qq.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();
}
}
上一篇:
Timestamp.java
下一篇:
黑色的摄影网站展示功能强大网上少见js技术强悍