json日期格式转换

  开发中,往往在后台把带有日期类型的一些数据直接转成json然后绑定到前台的样式列表控件里,在控件里获取或者绑定时,Date类型往往都变成了如
{"date":26,"day":1,"hours":11,"minutes":30,"month":9,"seconds":18,"time":1256527818296,"timezoneOffset":-480,"year":109}这样的格式,js获取时,为一个这样的对象而不是我们想要的“2014-11-11”这样的日期格式。

       如果我们这时我们就需要把这样的日起格式化成我们所常见的格式,如果想在JS页面使用,可使用下边方法:

定义一个如下方法:

[javascript]  view plain  copy
  1. function formatDate(v, dateFormat) {  
  2.     try {  
  3.         if (dateFormat == undefined || typeof dateFormat != "string") {  
  4.             dateFormat = "yyyy-MM-dd";  
  5.         }  
  6.         if ((typeof v) == "number"){  
  7.             var o = new Date(v*1000);  
  8.             return o.pattern(dateFormat);  
  9.         }  
  10.         if ((typeof v) == "string" && v.indexOf("/Date(") == 0) {  
  11.             var date = eval('new ' + eval(v).source);  
  12.             return date.pattern(dateFormat);  
  13.         }  
  14.         if (v.time) {  
  15.             var o = new Date(v.time);  
  16.             return o.pattern(dateFormat);  
  17.         }  
  18.         else {  
  19.             if (v != "") {  
  20.                 v = v.replace(/\//g, "-");  
  21.                 if(v=="1900-01-01 00:00:00"){  
  22.                     return "--";  
  23.                 }  
  24.                 if (v.split(" ")) {  
  25.                     var myDate = v.split(" ")[0];  
  26.                 } else {  
  27.                     var myDate = v;  
  28.                     var myTime = "";  
  29.                 }  
  30.                 myDate = myDate.replace("-0""-").replace("-0""-");  
  31.                 var nowDate = new Date();  
  32.                 /*TD 7111*/  
  33.                 if(myDate.split("-")[0]=="1900"){  
  34.                     return "--";  
  35.                 }  
  36.                 if (myDate.split("-")[0] == nowDate.getFullYear()) {//本年度 For td 5858  
  37.                     return myDate.split("-")[0] + "年" + myDate.split("-")[1] + "月" + myDate.split("-")[2] + "日";  
  38.                       
  39.                 } else {//非本年度  
  40.                     return myDate.split("-")[0] + "年" + myDate.split("-")[1] + "月" + myDate.split("-")[2] + "日";  
  41.                 }  
  42.             }else{  
  43.                 return "--";  
  44.             }  
  45.         }  
  46.     }catch (e) { }  
  47.     return "--";  
  48. };  

然后把对应的json日期字符串以参数形式传给定义好的函数formatDate,如果直接formatDate(jsonDate),就是返回默认的格式的日期如“2014-11-11”,如果这样使用formatDate(jsonDate,"yyyy年MM月dd日")可以以指定的日期格式返回。

       如果是在Java中遇到这样情况,想在java解决,在此引用一个网友的解决方法:

[java]  view plain  copy
  1. //注册date类型的转化方式  
  2.  JsonConfig jsonConfig = new JsonConfig();  
  3.  jsonConfig.registerJsonValueProcessor(java.util.Date.classnew JsonValueProcessorImplTest());  
  4.    
  5. JSONObject jsonFromBean = JSONObject.fromObject(testBean,jsonConfig);  
  6. System.out.println(jsonFromBean);  
  7.   
  8. //prints {"birthday":"2009-10-26","id":"id","name":"name"}  
  9.   
  10. String[] dateFormats = new String[] {"yyyy/MM/dd","yyyy-MM-dd"};   
  11. JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));   
  12. TestBean jsonToBean = (TestBean)JSONObject.toBean(jsonFromBean,TestBean.class);  
  13. System.out.println(jsonToBean);  
  14. //prints TestBean@1126b07[id=id,name=name,birthday=Mon Oct 26 00:00:00 CST 2009]  


 

其中需要的类如下:

 1.准备测试数据

[java]  view plain  copy
  1. import java.util.Date;  
  2.   
  3. import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
  4.   
  5. public class TestBean {  
  6.   
  7.     private String id;  
  8.     private String name;  
  9.     private java.util.Date birthday;  
  10.   
  11.     public TestBean() {  
  12.         super();  
  13.     }  
  14.   
  15.     public TestBean(String id, String name, Date birthday) {  
  16.         super();  
  17.         this.id = id;  
  18.         this.name = name;  
  19.         this.birthday = birthday;  
  20.     }  
  21.   
  22.     public String getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(String id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.   
  38.     public java.util.Date getBirthday() {  
  39.         return birthday;  
  40.     }  
  41.   
  42.     public void setBirthday(java.util.Date birthday) {  
  43.         this.birthday = birthday;  
  44.     }  
  45.   
  46.     public String toString() {  
  47.         return ReflectionToStringBuilder.toString(this);  
  48.     }  
  49.   
  50. }  

 

 

 

 

2.创建Date格式化类

[java]  view plain  copy
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3.   
  4. import net.sf.json.JsonConfig;  
  5. import net.sf.json.processors.JsonValueProcessor;  
  6.   
  7. public class JsonValueProcessorImplTest implements JsonValueProcessor {  
  8.     private String format = "yyyy-MM-dd";  
  9.   
  10.       
  11.     public JsonValueProcessorImplTest() {  
  12.         super();  
  13.     }  
  14.   
  15.     public JsonValueProcessorImplTest(String format) {  
  16.         super();  
  17.         this.format = format;  
  18.     }  
  19.   
  20.     @Override  
  21.     public Object processArrayValue(Object value, JsonConfig jsonConfig) {  
  22.         String[] obj = {};  
  23.         if (value instanceof Date[]) {  
  24.             SimpleDateFormat sf = new SimpleDateFormat(format);  
  25.             Date[] dates = (Date[]) value;  
  26.             obj = new String[dates.length];  
  27.             for (int i = 0; i < dates.length; i++) {  
  28.                 obj[i] = sf.format(dates[i]);  
  29.             }  
  30.         }  
  31.         return obj;  
  32.     }  
  33.   
  34.     @Override  
  35.     public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {  
  36.         if (value instanceof java.util.Date) {  
  37.             String str = new SimpleDateFormat(format).format((Date) value);  
  38.             return str;  
  39.         }  
  40.         return value.toString();  
  41.     }  
  42.   
  43.     public String getFormat() {  
  44.         return format;  
  45.     }  
  46.   
  47.     public void setFormat(String format) {  
  48.         this.format = format;  
  49.     }  
  50.   
  51. }  


通过如上方式我们就可以把含date类型的javabean在javabean和json格式中相互转化了,此java方法来自名为lyking2001

的ITeye上的网友。

       其实我们也可以把这个作为一个字符串分别拆分年、月、日来,然后简单计算(如用当前日期测试拆开得到的对应各个日期段的差值,然后就可以与真实的参数拆开进行计算)后拼在一起也是可以的,js和java都可以这样处理,只不过技术含量不高但简单易理解。

你可能感兴趣的:(java)