leftso 2171 0 2018-07-30 14:11:18

文章位置:左搜> 编程技术> 正文
fastJSON字符串类型数据中的日期转换为Java bean的日期对象



Person.java
import java.io.Serializable;
import java.util.Date;

public class Person implements Serializable {
    private String name;
    private Date birthday;
    private Date createDate;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}



Test.java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.text.SimpleDateFormat;

public class Test {
    public static void main(String[] args) {
        String personJSONStr="{\"name\":\"exampleName\",\"birthday\":\"2012-04-02\",\"createDate\":\"2018.09.22\"}";
        JSONObject jsonObject= JSON.parseObject(personJSONStr);
        Person person=jsonObject.toJavaObject(new TypeReference<Person>(){});
        System.out.println(new SimpleDateFormat("yyyMMdd").format(person.getCreateDate()));
    }
}


输出:
Connected to the target VM, address: '127.0.0.1:64532', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:64532', transport: 'socket'
Exception in thread "main" com.alibaba.fastjson.JSONException: For input string: "2018.09.22"
	at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1048)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:692)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:858)
	at com.alibaba.fastjson.JSON.toJavaObject(JSON.java:1019)
	at Test.main(Test.java:11)
Caused by: java.lang.NumberFormatException: For input string: "2018.09.22"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:589)
	at java.lang.Long.parseLong(Long.java:631)
	at com.alibaba.fastjson.util.TypeUtils.castToDate(TypeUtils.java:336)
	at com.alibaba.fastjson.util.TypeUtils.castToDate(TypeUtils.java:275)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:747)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:858)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:1180)
	at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1046)
	... 4 more

Process finished with exit code 1

解决办法:
修改person类:
import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;
import java.util.Date;

public class Person implements Serializable {
    private String name;

    @JSONField(format = "yyyy-MM-dd")
    private Date birthday;

    @JSONField(format = "yyyy.MM.dd")
    private Date createDate;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}