首页 > 代码库 > Java--Jackson转换Date,Timestamp 到格式化字符串

Java--Jackson转换Date,Timestamp 到格式化字符串

package com.diandaxia.test;import java.sql.Timestamp;import java.util.Date;/** * Created by del-berlin on 2017-03-07. */public class UserInfo {    private String name;    private int age;    private boolean sex;    private Date birthday;    private Timestamp end;    public Timestamp getEnd() {        return end;    }    public void setEnd(Timestamp end) {        this.end = end;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public boolean isSex() {        return sex;    }    public void setSex(boolean sex) {        this.sex = sex;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }}

 

技术分享

 

技术分享

package com.diandaxia.test;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by del-berlin on 2017-03-07. */public class TestDate {    public static void main(String[] args) {        UserInfo userInfo = new UserInfo();        userInfo.setName("小李子");        userInfo.setAge(10);        userInfo.setSex(true);        userInfo.setBirthday(new Date());        userInfo.setEnd(new Timestamp(new Date().getTime()));        ObjectMapper objectMapper = new ObjectMapper();        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));        try {            String json = objectMapper.writeValueAsString(userInfo);            System.out.println(json);        } catch (JsonProcessingException e) {            e.printStackTrace();        }    }}

 

Java--Jackson转换Date,Timestamp 到格式化字符串