欢迎访问我的个人网站==》 jiashubing.cn

FieldGroup绑定的日期类型存储格式的问题

问题

  日期存储的时候,当前数据库中存储格式为 "2017-9-5 0:00:00",
  而我实现了以后,看到数据库的存储格式为 "Mon Sep 04 00:00:00 CST 2017"

  原因找了很久,是在为FieldGroup 添加PropertysetItem 时的问题

 

比较

//第一种存储方式的实现为:
PropertysetItem item = new PropertysetItem();
item.addItemProperty("{code_act_Date1}",new ObjectProperty(""));
FieldGroup fieldGroup = new FieldGroup(item);

//第二种存储方式的实现为:
PropertysetItem item = new PropertysetItem();
item.addItemProperty("{code_act_Date1}",new DateField());
FieldGroup fieldGroup = new FieldGroup(item);

  这两种实现,在下面的代码中会走不同的判断,因为他们的Type不一样,而这个Type不能直接set,只能根据上面的那种方法添加。

if (propertysetItem.getItemProperty(key).getType() == Date.class) {
    Date date = new Date((String) mapValues.get(key));
    propertysetItem.getItemProperty(key).setValue(date);
} else {
    propertysetItem.getItemProperty(key).setValue(mapValues.get(key));
}

   奇葩就奇葩在,直接把日期类型存储为字符串类型,居然也没问题,可以正好的转换成"yyyy-MM-dd HH:mm:ss"格式。

  而且事实证明,所有情况下都没有走过getType() == Date.class这个判断。我就觉得奇怪嘛,没用到日期格式化,直接把String转换成Date类型居然不报错,原来是从来没走过这个方法。 

 

比较两种类型保存在FieldGroup中的区别

new DateField()

 

new ObjectProperty("")

posted @ 2017-09-13 15:36  贾树丙  阅读(359)  评论(0编辑  收藏  举报