http://www.javaeye.com/problems/41710
用Java的Json-lib输出的timestamp格式如下:
:{"date":6,"day":4,"hours":16,"minutes":46,"month":4,"nanos":0,"seconds":35,"time":1273135595000,"timezoneOffset":-480,"year":110}
请问如何转换成ExtJS可以显示的格式,或用ExtJS如何直接转换。
Json-lib输出的这种日期时间型Json,不像Rails一样容易转换。
- package com.util;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import net.sf.json.JsonConfig;
- import net.sf.json.processors.JsonValueProcessor;
- public class DateJsonValueProcessor implements JsonValueProcessor {
- public static final String Default_DATE_PATTERN ="yyyy-MM-dd";
- private DateFormat dateFormat ;
- public DateJsonValueProcessor(String datePattern){
- try{
- dateFormat = new SimpleDateFormat(datePattern);
- }catch(Exception e ){
- dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
- }
- }
- public Object processArrayValue(Object value, JsonConfig jsonConfig) {
- return process(value);
- }
- public Object processObjectValue(String key, Object value,
- JsonConfig jsonConfig) {
- return process(value);
- }
- private Object process(Object value){
- return dateFormat.format((Date)value);
- }
- }
- JsonConfig config = new JsonConfig();
- config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));