1. Normal way:
The default is to turn into timestamps form, the timestamps can be canceled by the following way.
Objectmapper.configure (SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
This will enable time generation using the so-called use a [ISO-8601]-compliant notation, which outputs a time similar to the following format: "1970-01-01t00:00:00.000+0000".
Of course, you can also customize the output format:
Objectmapper.getserializationconfig (). Setdateformat (Mydateformat);
Mydateformat object is Java.text.DateFormat, use inventory Java API specifically
SimpleDateFormat mydateformat=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Objectmapper.getserializationconfig (). Setdateformat (Mydateformat);
How to annotate 2.annotaion:
First define the format you want, for example:
Java code
Import Java.io.ioexception;import Java.text.simpledateformat;import Java.util.date;import Org.codehaus.jackson.jsongenerator;import Org.codehaus.jackson.jsonprocessingexception;import Org.codehaus.jackson.map.jsonserializer;import org.codehaus.jackson.map.serializerprovider;/** * Java Date object converted to JSON date formatted custom class by Jackson Library * @author Yuruli * @date 2010-7-25 */public class Customdateserializer extends Jsonser Ializer { @Override public void Serialize (Date value, Jsongenerator Jgen, Serializerprovider provider) throws IOException, jsonprocessingexception { SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd"); String formatteddate = Formatter.format (value); Jgen.writestring (formatteddate); }}
and find the date's Get method on your Pojo.
@JsonSerialize (using = customdateserializer.class) public Date getcreateat () { return createat;}
Date format setting when converting JSON with Jackson