1

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;

}

My record

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

My result in firefox

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

Question is, Why 3918-02-01? and how to fix it;

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
Sumek
  • 11
  • 4
  • Welcome to StackOverflow. Please take the [Tour](https://stackoverflow.com/tour) to see how you can edit your question to be more informative and clear, in order to enable other users to answer it. For further details, please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236). – zyndor Dec 29 '17 at 10:53
  • You are using a long deprecated `Date` constructor. Best if you can avoid using `Date` at all. [The modern Java date and time API known as `java.time` (or JSR 310)](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. – Ole V.V. Dec 29 '17 at 11:04
  • Whenever I need an old-fashioned `Date` object with a specific value, I use like `Date.from(ZonedDateTime.of(2018, 1, 1, 12, 44, 0, 0, ZoneId.of("Europe/Berlin")).toInstant())`. It’s longer, but it’s clearer to read, hard to get wrong and it specifies more precisely what I want and get. – Ole V.V. Dec 29 '17 at 11:38

2 Answers2

1

According to JavaDoc this Date constructor takes year argument as year-1900, thus your initial year is getting added by 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.

This is also a deprecated long time ago. I would suggest using LocalDateTime from here

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
  • Seems to be good however it result in date:{ year:2018 month:"JANUARY" monthValue:1 dayOfMonth:1 hour:12 minute:44 second:0 nano:0 dayOfWeek:"MONDAY" dayOfYear:1 chronology:{…}} And i need just year,month,day,hour,minutes any tip please? – Sumek Dec 29 '17 at 11:15
  • 1
    @Sumek which method have you chosen. Probably you need `LocalDateTime.of`. I bet you can find out which one is suitable for you from the source code here: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html – Shubhendu Pramanik Dec 29 '17 at 12:05
  • I think about output not about methods. I have already done this, thanks for help. Link to the output problem for the future. https://stackoverflow.com/questions/26804941/how-can-i-have-jax-rs-return-a-java-8-localdatetime-property-as-a-javascript-sty – Sumek Dec 29 '17 at 14:26
0

Date(int year, int month, int date, int hrs, int min) Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min).

you can use then:

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();

or

Calendar cal = Calendar.getInstance();
cal.set(2018,1,1,12,44);
Date date = cal.getTime();
krezus
  • 1,281
  • 1
  • 13
  • 29