如何使用faster.xml在转换后的json中获取java.util.Date和java.sql.Timestamp的不同模式

时间:2016-01-25 07:10:08

标签: java jackson fasterxml

您好我使用的是speedxml 2.6.1将java转换为json,其中java类是使用xsds生成的,如下所示

javaclass

@XmlAccessorType(XmlAccessType.FIELD)

@Generated(value =“com.sun.tools.xjc.Driver”,date =“2016-01

23T03:04:56+05:30", comments = "JAXB RI v2.2.11")

公共类TestBean {

@XmlElement(type = String.class)
@XmlSchemaType(name = "date")
@Generated(value = "com.sun.tools.xjc.Driver", date = "2016-01-23T03:04:56+05:30", comments = "JAXB RI v2.2.11")
private Date date ;

@XmlElement(type = String.class)
@XmlSchemaType(name = "date")
@Generated(value = "com.sun.tools.xjc.Driver", date = "2016-01-23T03:04:56+05:30", comments = "JAXB RI v2.2.11")
private Timestamp timestamp;

 @Generated(value = "com.sun.tools.xjc.Driver", date = "2016-01-23T03:04:56+05:30", comments = "JAXB RI v2.2.11")
public Date getDate() {
    return date;
}

 @Generated(value = "com.sun.tools.xjc.Driver", date = "2016-01-23T03:04:56+05:30", comments = "JAXB RI v2.2.11")
public void setDate(Date date) {
    this.date = date;
}

 @Generated(value = "com.sun.tools.xjc.Driver", date = "2016-01-23T03:04:56+05:30", comments = "JAXB RI v2.2.11")
public Timestamp getTimestamp() {
    return timestamp;
}

 @Generated(value = "com.sun.tools.xjc.Driver", date = "2016-01-23T03:04:56+05:30", comments = "JAXB RI v2.2.11")
public void setTimestamp(Timestamp timestamp) {
    this.timestamp = timestamp;
}
}

如果我将java转换为json默认情况下我会得到时间,所以我将日期格式设置为“YYYYY-MM-DD”。

public class TestJson {

public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    Date date = new Date();

    TestBean testBean = new TestBean();
    testBean.setDate(date);
    Timestamp timestamp = new Timestamp(date.getTime());
    testBean.setTimestamp(timestamp);
    SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
    mapper.setDateFormat(dateFormat);
    dateFormat.setLenient(false);

      System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(testBean));

}

但是我想为Date和Timestamp获得不同的模式       例如日期YYYY-MM-DD(2016-01-25)            对于时间戳dd / MM / yyyy hh:mm:ss.SSS 是否有任何配置可用更快的xml来获得不同的模式?

0 个答案:

没有答案