开发中,往往在后台把带有日期类型的一些数据直接转成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页面使用,可使用下边方法:
定义一个如下方法:
- function formatDate(v, dateFormat) {
- try {
- if (dateFormat == undefined || typeof dateFormat != "string") {
- dateFormat = "yyyy-MM-dd";
- }
- if ((typeof v) == "number"){
- var o = new Date(v*1000);
- return o.pattern(dateFormat);
- }
- if ((typeof v) == "string" && v.indexOf("/Date(") == 0) {
- var date = eval('new ' + eval(v).source);
- return date.pattern(dateFormat);
- }
- if (v.time) {
- var o = new Date(v.time);
- return o.pattern(dateFormat);
- }
- else {
- if (v != "") {
- v = v.replace(/\
- if(v=="1900-01-01 00:00:00"){
- return "--";
- }
- if (v.split(" ")) {
- var myDate = v.split(" ")[0];
- } else {
- var myDate = v;
- var myTime = "";
- }
- myDate = myDate.replace("-0", "-").replace("-0", "-");
- var nowDate = new Date();
-
- if(myDate.split("-")[0]=="1900"){
- return "--";
- }
- if (myDate.split("-")[0] == nowDate.getFullYear()) {
- return myDate.split("-")[0] + "年" + myDate.split("-")[1] + "月" + myDate.split("-")[2] + "日";
-
- } else {
- return myDate.split("-")[0] + "年" + myDate.split("-")[1] + "月" + myDate.split("-")[2] + "日";
- }
- }else{
- return "--";
- }
- }
- }catch (e) { }
- return "--";
- };
然后把对应的json日期字符串以参数形式传给定义好的函数formatDate,如果直接formatDate(jsonDate),就是返回默认的格式的日期如“2014-11-11”,如果这样使用formatDate(jsonDate,"yyyy年MM月dd日")可以以指定的日期格式返回。
如果是在Java中遇到这样情况,想在java解决,在此引用一个网友的解决方法:
-
- JsonConfig jsonConfig = new JsonConfig();
- jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImplTest());
-
- JSONObject jsonFromBean = JSONObject.fromObject(testBean,jsonConfig);
- System.out.println(jsonFromBean);
-
-
-
- String[] dateFormats = new String[] {"yyyy/MM/dd","yyyy-MM-dd"};
- JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
- TestBean jsonToBean = (TestBean)JSONObject.toBean(jsonFromBean,TestBean.class);
- System.out.println(jsonToBean);
-
其中需要的类如下:
1.准备测试数据
- import java.util.Date;
-
- import org.apache.commons.lang.builder.ReflectionToStringBuilder;
-
- public class TestBean {
-
- private String id;
- private String name;
- private java.util.Date birthday;
-
- public TestBean() {
- super();
- }
-
- public TestBean(String id, String name, Date birthday) {
- super();
- this.id = id;
- this.name = name;
- this.birthday = birthday;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public java.util.Date getBirthday() {
- return birthday;
- }
-
- public void setBirthday(java.util.Date birthday) {
- this.birthday = birthday;
- }
-
- public String toString() {
- return ReflectionToStringBuilder.toString(this);
- }
-
- }
2.创建Date格式化类
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import net.sf.json.JsonConfig;
- import net.sf.json.processors.JsonValueProcessor;
-
- public class JsonValueProcessorImplTest implements JsonValueProcessor {
- private String format = "yyyy-MM-dd";
-
-
- public JsonValueProcessorImplTest() {
- super();
- }
-
- public JsonValueProcessorImplTest(String format) {
- super();
- this.format = format;
- }
-
- @Override
- public Object processArrayValue(Object value, JsonConfig jsonConfig) {
- String[] obj = {};
- if (value instanceof Date[]) {
- SimpleDateFormat sf = new SimpleDateFormat(format);
- Date[] dates = (Date[]) value;
- obj = new String[dates.length];
- for (int i = 0; i < dates.length; i++) {
- obj[i] = sf.format(dates[i]);
- }
- }
- return obj;
- }
-
- @Override
- public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
- if (value instanceof java.util.Date) {
- String str = new SimpleDateFormat(format).format((Date) value);
- return str;
- }
- return value.toString();
- }
-
- public String getFormat() {
- return format;
- }
-
- public void setFormat(String format) {
- this.format = format;
- }
-
- }
通过如上方式我们就可以把含date类型的javabean在javabean和json格式中相互转化了,此java方法来自名为lyking2001
的ITeye上的网友。
其实我们也可以把这个作为一个字符串分别拆分年、月、日来,然后简单计算(如用当前日期测试拆开得到的对应各个日期段的差值,然后就可以与真实的参数拆开进行计算)后拼在一起也是可以的,js和java都可以这样处理,只不过技术含量不高但简单易理解。