首页 / 操作系统 / Linux / Json关于java.sql.Date的处理
步骤一 、 VO类import java.sql.Date; public class User { //实体类的属性和表的字段名称一一对应 private int id; private String name; private int age; private Date hire_date;} 步骤二 、数据库表:/*Navicat MySQL Data TransferSource Server : localhostSource Server Version : 50051Source Host : localhost:3306Source Database : test Target Server Type : MYSQLTarget Server Version : 50051File Encoding : 65001Date: 2016-05-17 20:46:04*/ SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for `user`-- ----------------------------DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `id` int(11) NOT NULL auto_increment, `name` varchar(30) default NULL, `age` int(11) default NULL, `hire_date` datetime default NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ------------------------------ Records of user-- ----------------------------INSERT INTO `user` VALUES ("1", "zhangsan", "12", "2016-05-17 20:06:38"); 步骤三、开始转换Exception in thread "main" net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)at net.sf.json.JSONObject._fromBean(JSONObject.java:699)at net.sf.json.JSONObject.fromObject(JSONObject.java:172)at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)at net.sf.json.JSONObject._processValue(JSONObject.java:2655)at net.sf.json.JSONObject.processValue(JSONObject.java:2721)at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)at net.sf.json.JSONObject.setValue(JSONObject.java:1424)at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)at net.sf.json.JSONObject._fromBean(JSONObject.java:699)at net.sf.json.JSONObject.fromObject(JSONObject.java:172)at net.sf.json.JSONObject.fromObject(JSONObject.java:134)at mybatistest.mybatisdemo.MultiDataSource.main(MultiDataSource.java:50)
解决方案一:datetime -->String select id,name ,age,CONCAT(hire_date,"") from user 解决方案二:增加函数转为util.Date进行处理 /** * 将java.util.Date日期转化为java.sql.Date * @param udate * @return */ public static java.sql.Date converUtilToSql(java.util.Date udate) { return new java.sql.Date(udate.getTime()); } /** * 将java.sql.Date日期转化为java.util.Date * @param udate * @return */ public static java.util.Date converSqlToUtil(java.sql.Date udate) { return new java.util.Date(udate.getTime()); }
处理结果如下:{"age":12,"hire_date":{"date":17,"day":2,"hours":0,"minutes":0,"month":4,"seconds":0,"time":1463414400000,"timezoneOffset":-480,"year":116},"id":1,"name":"zhangsan"} 解决方案三:自定义转换器核心代码如下: JsonConfig jsonConfig = new JsonConfig();jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());JSONObject json = JSONObject.fromObject(user,jsonConfig);System.out.println("=====total count getUser====" + json); public class JsonDateValueProcessor implements JsonValueProcessor { private String format = "yyyy-MM-dd"; public Object processArrayValue(Object arg0, JsonConfig arg1) {System.out.println("====processArrayValue======== "+arg0);return null; } public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {System.out.println("====processObjectValue======== "+arg0 +",arg1="+arg1);return processJsonDateValue(arg1); } private Object processJsonDateValue(Object arg0) {SimpleDateFormat sdf=new SimpleDateFormat(format);return sdf.format(arg0);} }处理结果如下:=====total count getUser====User [id=1, name=zhangsan, age=12,hire_date=2016-05-17]====processObjectValue======== hire_date,arg1=2016-05-17=====total count getUser===={"age":12,"hire_date":"2016-05-17","id":1,"name":"zhangsan"}
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-05/131493.htm