首页 > 代码库 > 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 到格式化字符串
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。