当前位置:编程学习 > JS >>

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

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

代码如下:
[java] 
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会把时间分别按年,月,日,时,分,秒属性显示,然不是我们想要的完整的时间字符串,这怎么办呢?后来想了一招,自己手动格式化字符串,代码如下:

[java] 
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格式。

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,