son与Bean互转,Timestamp类型的问题

Posted on 2015-02-14 09:58 RyanChan 阅读(61) 评论(0) 编辑 收藏

Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器。

DateJsonValueProcessor的作用是Bean转换为Json时将Timepstamp转换为指定的时间格式。

 1 import java.text.DateFormat;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 import net.sf.json.JsonConfig;
 5 import net.sf.json.processors.JsonValueProcessor;
 6 /*** 将Bean中的Timestamp转换为json中的日期字符串*/
 7 public class DateJsonValueProcessor implements JsonValueProcessor {
 8     public static final String Default_DATE_PATTERN ="yyyy-MM-dd";
 9     private DateFormat dateFormat ;
10     public DateJsonValueProcessor(String datePattern){
11         try{
12             dateFormat  = new SimpleDateFormat(datePattern);}
13         catch(Exception e ){
14             dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
15         }
16     }
17     public Object processArrayValue(Object value, JsonConfig jsonConfig) {
18         return process(value);
19     }
20     public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
21         return process(value);
22     }
23     private Object process(Object value){
24         return dateFormat.format((Date)value);
25     }
26 }

TimestampMorpher的作用则与DateJsonValueProcessor相反,它是在Jsonl转换为Bean时,会把指定的时间格式转换为Timestamp

 1 import java.sql.Timestamp;
 2 import java.text.ParseException;
 3 import java.text.SimpleDateFormat;
 4 import net.sf.ezmorph.MorphException;
 5 import net.sf.ezmorph.object.AbstractObjectMorpher;
 6 /*** 将json串中的日期字符串转换为bean中的Timestamp*/
 7 public class TimestampMorpher extends AbstractObjectMorpher {
 8 /*** 日期字符串格式*/
 9 private String[] formats;
10 public TimestampMorpher(String[] formats) {
11     this.formats = formats;
12 }
13 public Object morph(Object value) {
14 if( value == null){
15     return null;
16 }
17 if( Timestamp.class.isAssignableFrom(value.getClass()) ){
18     return (Timestamp) value;
19 }
20 if( !supports( value.getClass()) ){
21     throw new MorphException( value.getClass() + " 是不支持的类型");
22 }
23 String strValue=(String) value;
24 SimpleDateFormat dateParser=null;
25 for( int i = 0; i < formats.length ; i++ ){
26     if( null == dateParser ){
27         dateParser=new SimpleDateFormat(formats[i]);
28     }else{
29         dateParser.applyPattern(formats[i]);
30     }
31     try{
32         return new Timestamp( dateParser.parse( strValue.toLowerCase()).getTime() );}
33     catch (ParseException e) {
34         //e.printStackTrace();
35     }
36 }
37 return null;
38 }
39 @Override
40 public Class morphsTo() {
41     return Timestamp.class;
42 }
43 public boolean supports( Class clazz ){
44     return String.class.isAssignableFrom( clazz );
45 }
46
47 }

测试一下。

http://www.etf88.com/djxw/srys/2015/0214/206758.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206757.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206756.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206755.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206754.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206752.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206750.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206748.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206746.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206744.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206742.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206740.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206739.shtml
  http://www.etf88.com/djxw/srys/2015/0214/206738.shtml
  http://www.whtv.com.cn/bjyp/scyy/2015021414057.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414056.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414055.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414054.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414053.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414052.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414051.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414050.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414049.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414048.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414047.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414046.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414045.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414044.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414043.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414042.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414041.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414040.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414039.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414038.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414037.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414036.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414035.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414034.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414033.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414032.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414031.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414030.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414029.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414028.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414027.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414026.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414025.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414024.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414023.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414022.html
  http://www.whtv.com.cn/bjyp/scyy/2015021414021.html
  http://www.zhnews.net/kpzx/bhwd/8013719066.html
  http://www.zhnews.net/kpzx/bhwd/8013714847.html
  http://www.zhnews.net/kpzx/bhwd/8013714675.html
  http://www.zhnews.net/kpzx/bhwd/8013714291.html
  http://www.zhnews.net/kpzx/bhwd/8013714391.html
  http://www.zhnews.net/kpzx/bhwd/8013714055.html
  http://www.zhnews.net/kpzx/bhwd/8013714711.html
  http://www.zhnews.net/kpzx/bhwd/8013719102.html
  http://www.zhnews.net/kpzx/bhwd/8013718855.html
  http://www.zhnews.net/kpzx/bhwd/8013718443.html
  http://www.zhnews.net/kpzx/bhwd/8013718307.html
  http://www.zhnews.net/kpzx/bhwd/8013718168.html
  http://www.zhnews.net/kpzx/bhwd/8013721346.html
  http://www.zhnews.net/kpzx/bhwd/8013719202.html
  http://www.51etong.com/kpzx/jkzn/7687505400.html
  http://www.51etong.com/kpzx/jkzn/7687505384.html
  http://www.51etong.com/kpzx/jkzn/7687505387.html
  http://www.51etong.com/kpzx/jkzn/7687505385.html
  http://www.51etong.com/kpzx/jkzn/7687505386.html
  http://www.51etong.com/kpzx/jkzn/7687505383.html
  http://www.51etong.com/kpzx/jkzn/7687505382.html
  http://www.51etong.com/kpzx/jkzn/7687505394.html
  http://www.51etong.com/kpzx/jkzn/7687505393.html
  http://www.51etong.com/kpzx/jkzn/7687505392.html
  http://www.51etong.com/kpzx/jkzn/7687505390.html
  http://www.51etong.com/kpzx/jkzn/7687505391.html
  http://www.51etong.com/kpzx/jkzn/7687505389.html
  http://www.51etong.com/kpzx/jkzn/7687505388.html
  http://www.51etong.com/kpzx/jkzn/7687505399.html
  http://www.51etong.com/kpzx/jkzn/7687505398.html
  http://www.51etong.com/kpzx/jkzn/7687505397.html
  http://www.51etong.com/kpzx/jkzn/7687505396.html
  http://www.51etong.com/kpzx/jkzn/7687505395.html
  http://www.lzbs.com.cn/yszx/sjst/8012949666.html
  http://www.lzbs.com.cn/yszx/sjst/8012949347.html
  http://www.lzbs.com.cn/yszx/sjst/8012949530.html
  http://www.lzbs.com.cn/yszx/sjst/8012950122.html
  http://www.lzbs.com.cn/yszx/sjst/8012953555.html
  http://www.lzbs.com.cn/yszx/sjst/8012948368.html
  http://www.lzbs.com.cn/yszx/sjst/8012948196.html
  http://www.lzbs.com.cn/yszx/sjst/8012947922.html
  http://www.lzbs.com.cn/yszx/sjst/8012947566.html
  http://www.lzbs.com.cn/yszx/sjst/8012947366.html
  http://www.lzbs.com.cn/yszx/sjst/8012947466.html
  http://www.lzbs.com.cn/yszx/sjst/8012947111.html
  http://www.lzbs.com.cn/yszx/sjst/8012950551.html
  http://www.lzbs.com.cn/yszx/sjst/8012950341.html
  http://www.lzbs.com.cn/yszx/sjst/8012949766.html
  http://www.020.com/ys/sjys/8013834846.html
  http://www.020.com/ys/sjys/8013834098.html
  http://www.020.com/ys/sjys/8013833762.html
  http://www.020.com/ys/sjys/8013836327.html
  http://www.020.com/ys/sjys/8013836462.html
  http://www.020.com/ys/sjys/8013833527.html
  http://www.020.com/ys/sjys/8013836896.html
  http://www.020.com/ys/sjys/8013833662.html
  http://www.020.com/ys/sjys/8013832396.html
  http://www.020.com/ys/sjys/8013835296.html
  http://www.020.com/ys/sjys/8013832138.html
  http://www.020.com/ys/sjys/8013832496.html
  http://www.020.com/ys/sjys/8013831966.html
  http://www.020.com/ys/sjys/8013834982.html

时间: 2024-10-06 13:47:33

son与Bean互转,Timestamp类型的问题的相关文章

Json与Bean互转,Timestamp类型的问题

Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器. DateJsonValueProcessor的作用是Bean转换为Json时将Timepstamp转换为指定的时间格式. 1 import java.text.DateFormat; 2 import java.text.SimpleDateFormat; 3 import java.util.Date; 4 import net.sf.json.JsonConfig; 5 impo

使用谷歌Gson实现Json串和Java Bean互转

/** * 使用谷歌Gson实现Json串和Java Bean互转 */ public class JsonHelper { public static String toJson(Object src){ return new Gson().toJson(src); } public static <T> T fromJson(String json, Class<T> clazz) throws Exception { return new Gson().fromJson(js

MySQL的timestamp类型自动更新问题

今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化. 查找资料,发现是我建表的语句有问题: 以下是代码片段: CREATE TABLE `test` ( `t1` timestamp NOT NULL default CURRENT_TIMESTAMP, `ww` varchar(5) NOT NULL ) ENGINE=InnoDB DEFAUL

Oracle插入timestamp类型数据

表结构: create table TEST( ID INTEGER, BIRTHDAY TIMESTAMP ); 使用JDBC将日期插入到TIMESTAMP类型字段 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.SimpleDateFormat; import

在hive中直接对timestamp类型取max报错

之前直接对timestamp类型做max操作, select id,max(updatetime) updatetime from his.tag group by id; 结果查询的结果有的显示为1970-01-01,如下图: 解决办法: 先将timestamp类型用unix_timestamp函数转换成bigint 然后进行取max操作,最后再将结果通过from_unixtime函数转换成string select id,from_unixtime(max(unix_timestamp(up

MySQL中DATETIME、DATE和TIMESTAMP类型的区别

一.TIMESTAMP 显示格式:YYYY-MM-DD HH:MM:SS 时间范围:[ '1970-01-01 00:00:00'到'2037-12-31 23:59:59'] TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP  在创建新记录和修改现有记录的时候都对这个数据列刷新. TIMESTAMP DEFAULT CURRENT_TIMESTAMP  在创建新记录的时候把这个字段设置为当前时间,但以后修改时,不再刷

mysql关于timestamp类型

数据库设置时间字段为timestamp类型,DTO设置为java.util.Date类型.DTO设值sessionLogDTO.setLoginOutTime(new Date());用new Date()即可:效果如右图

java 反射: 当Timestamp类型的属性值为null时,设置默认值

import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.sql.Timestamp; class Person { private String name; private int age; private Timestamp birth; public Timestamp getBirth() { return birth

Oracle中,将毫秒数转换为timestamp类型的两种方法

在许多场景中,开发人员习惯用1970-01-01 00:00:00.000以来的毫秒数来表示具体的时间,这样可以将数据以NUMBER类型存储到数据库中,在某些时候方便比较,同样,有些时候我们需要 把这种毫秒数转换成标准的TIMESTAMP类型,现在总结了两种实现方法: 方法一: SELECT TO_TIMESTAMP('1970-01-01 00:00:00.000','yyyy-MM-dd hh24:mi:ss.ff3')+1397457489296/1000/60/60/24 FROM du