java 日期转化器,BeanUtils.copyProperties日期转换器DateConverter

BeanUtils.copyProperties日期转换器DateConverter

a4c26d1e5885305701be709a3d33442f.png

(2012-05-13 21:56:22)

标签:

java

web

it

在运用BeanUtils.copyProperties的时候,如果遇到日期和字符串彼此转换的,需要自定义日期转换器,这里提供了㈠个日期转换器,并且演示了其运用,同时演示了2种作用范围的用法:全局、局簿?局部范围的用法可以支持特别的情况——比如有2种矛盾的日期转换器需求的情况。

import java.text.ParseException;

import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.BeanUtilsBean;

import org.apache.commons.beanutils.ConvertUtils;

import org.apache.commons.beanutils.ConvertUtilsBean;

import org.apache.commons.beanutils.Converter;

import org.apache.commons.beanutils.PropertyUtilsBean;

import org.apache.commons.lang.time.DateUtils; a4c26d1e5885305701be709a3d33442f.png

public class DateConverter implements Converter {

public static void main(String[] a) {

D1 d1 = new D1();

d1.setDate("2008-11-21 15:45:59");

//私用的指建转换器

ConvertUtilsBean convertUtils = new ConvertUtilsBean();//

java自动转换的工具类

DateConverter dateConverter = new DateConverter();//

实力㈠个日期转换类

convertUtils.register(dateConverter, Date.class);// 注册㈠个日期类

convertUtils.register(dateConverter, String.class);// 注册㈠个字符类

BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,

new PropertyUtilsBean());// 将这个转换工具类加载到beanUtils属性中

{

D2 d2 = new D2();

try {

beanUtils.copyProperties(d2, d1);

System.out.println(d2.getDate());

} catch (Exception e) {

System.err.println("1.没指建时间转换器");

}

}

//公用的那个因为没有指建转换器

{

D2 d2 = new D2();

try {

BeanUtils.copyProperties(d2, d1);

System.out.println(d2.getDate());

} catch (Exception e) {

System.err.println("2.没指建时间转换器");

}

}

//公用的指建转换器

{

ConvertUtils.register(dateConverter, Date.class);// 注册㈠个日期类

ConvertUtils.register(dateConverter, String.class);// 注册㈠个字符类

D2 d2 = new D2();

try {

BeanUtils.copyProperties(d2, d1);

System.out.println(d2.getDate());

} catch (Exception e) {

System.out.println("3.没指建时间转换器");

}

}

}

public Object convert(Class type, Object value) {

if (value == null) {

return null;

}

if (value instanceof Date) {

return value;

}

if (value instanceof Long) {

Long longValue = (Long) value;

return new Date(longValue.longValue());

}

if (value instanceof String) {

Date endTime = null;

try {

endTime = DateUtils.parseDate(value.toString(), new String[]

{

"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss",

"yyyy-MM-dd HH:mm" });

} catch (ParseException e) {

e.printStackTrace();

}

return endTime;

}

return null;

}

}

public class D1 {

private String date = null;

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

}

import java.util.Date;

public class D2 {

private Date date = null;

public Date getDate() {

return date;

}

public void setDate(Date date) {

this.date = date;

}

}

分享:

a4c26d1e5885305701be709a3d33442f.png喜欢

0

a4c26d1e5885305701be709a3d33442f.png赠金笔

加载中,请稍候......

评论加载中,请稍候...

发评论

登录名: 密码: 找回密码 注册记住登录状态

昵   称:

评论并转载此博文

a4c26d1e5885305701be709a3d33442f.png

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值