使用BeanUtils.describe()把日期格式化

public class A{
    private String id;
    private Date createTime;

    public A(String id, Date createTime) {
        this.id = id;
        this.createTime = createTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
Map<String,String> map= BeanUtils.describe(new A("123",new Date()));
System.out.println(map.get("createTime"));
System.out.println(map.get("id"));
输出为

Mon Mar 12 17:50:34 CST 2018
123

但是我想把日期的格式改下格式

        ConvertUtilsBean convertUtils = BeanUtilsBean.getInstance().getConvertUtils();
        DateConverter dateConverter = new DateConverter();
        dateConverter.setPattern("yyyy-MM-dd HH:mm:ss");
        convertUtils.register(dateConverter, String.class);
        Map<String,String> map= BeanUtils.describe(new A("123",new Date()));
        System.out.println(map.get("createTime"));
        System.out.println(map.get("id"));
输出为

2018-03-12 17:53:25
123

你可能感兴趣的:(编程技巧)