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?以及如何解决它;
答案 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();