Jax-RS JsonFormat日期未知39

时间:2017-12-29 10:43:01

标签: java rest maven date jax-rs

FilmEvent.java

    import java.util.Date;

    @AllArgsConstructor(access=AccessLevel.PUBLIC)
    public class FilmEvent {
    @Getter
    private String id;

    @Getter
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:mm", timezone="CET")
    private Date date;

    @Getter
    private String[] attributes;

    @Getter
    private boolean soldOut;

}

我的记录

FilmEvent event = new FilmEvent("334",  new Date(2018,1,1,12,44), null, false);

我在firefox中的结果

date:"3918-02-01,12:44"

问题是,为什么选择3918-02-01?以及如何解决它;

2 个答案:

答案 0 :(得分:1)

根据JavaDoc,此Date构造函数将年份参数视为year-1900,因此您的初始年份将由1900添加。

<强>的JavaDoc:

year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.
hrs - the hours between 0-23.
min - the minutes between 0-59.

这也是很久以前的弃用。我建议您使用here

中的LocalDateTime

答案 1 :(得分:0)

  

日期(int year,int month,int date,int hrs,int min)不推荐使用。如   JDK 1.1版,取而代之的是Calendar.set(年+ 1900,月,日,   小时,分钟)或GregorianCalendar(年+ 1900,月,日,小时,分钟)。

你可以使用:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MINUTE, 12);
cal.set(Calendar.SECOND, 44);
Date date = cal.getTime();

Calendar cal = Calendar.getInstance();
cal.set(2018,1,1,12,44);
Date date = cal.getTime();