etmvc中生成JsonView的时间格式问题处理

本人在etmvc中使用ActiveRecord来实现ORM,结果在生成JsonView返回时,发现日期时间格式全变成了日期没有时间,这是为什么呢?

代码如下:

	public JsonView getLogs(int rows, int page
			) throws Exception {
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		
		long total = Log.count(Log.class, cond, args);	//查询总数量
		List<Log> logs = Log.findAll(Log.class, cond, args, "id", rows, (page-1)*rows);	//查询一页资料
		
		//构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", logs);
		//System.out.println(result.toString());
		JsonView view = new JsonView(result);
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

结果返回如下,时间没了,只有日期:

网上找了老半天也没找着说这事的,后来大概猜测了一下,可能是默认情况下JsonView会把时间分别按年,月,日,时,分,秒属性显示,然不是我们想要的完整的时间字符串,这怎么办呢?后来想了一招,自己手动格式化字符串,代码如下:

	public JsonView getLogs(int rows, int page
			) throws Exception {
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		
		long total = Log.count(Log.class, cond, args);	//查询总数量
		List<Log> logs = Log.findAll(Log.class, cond, args, "id", rows, (page-1)*rows);	//查询一页资料
		
		List<Map<String,Object>> _list=new ArrayList<Map<String,Object>>();
		for(Log log: logs){
			Map<String,Object> _map=new HashMap<String,Object>();
			_map.put("id", log.getId());
			_map.put("userName", log.getUserName());
			_map.put("userIP", log.getUserIP());
			_map.put("logTime", log.getLogTime().toString());
			_list.add(_map);
		}
		
		//构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", _list);
		//System.out.println(result.toString());
		JsonView view = new JsonView(result);
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

结果返回:

问题暂且搞定,不知道是不是还有其他办法,目前尚未研究!顺带提一下,数据库是MySQL,日期时间格式是:datetime,model里用的是java.sql.Timestamp格式。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值