0

In my model I have the following:

private Date exampleDate;

public Testing(Date exampleDate) {
        this.exampleDate = exampleDate;
    }

public Date getExampleDate() {
        return (Date) this.exampleDate.clone();
    }

    public void setExampleDate(Date exampleDate) {
        this.exampleDate = (Date) exampleDate.clone();
    }

And I am trying to output an array of objects as follows

@RestController
public class TestingController {
    @GetMapping("/saved")
    public List<Testing> getData() {
        List<Testing> savedDatas = new ArrayList<>(Arrays.asList(
                new Testing(
                        1,
                        "Text",
                        2000-1- 1),
                new Testing(
                        2,
                        "Text2",
                        new Date(27 / 03 / 19)),

I tried to use new Date but that doesn't work, so what exactly do I need to pass as the third argument as type Date instead of String or int?!?!

5
  • What package does Date come from? Is it java.util.Date? Commented Sep 21, 2020 at 11:27
  • @Nikolas yes... Commented Sep 21, 2020 at 11:28
  • 1
    When you use Java 8 then PLEASE don't use the 20 year old deprecated java.util.Date. Use java.time.LocalDate instead Commented Sep 21, 2020 at 11:29
  • Your Question is not clear. plz share what exception do you get? Commented Sep 21, 2020 at 11:30
  • 1
    Consider using LocalDate or LocalDateTime instead as of java-8. Commented Sep 21, 2020 at 11:30

1 Answer 1

1

I would suggest you use the newer classes:

LocalDateTime

and

ZonedDateTime

Then you can get a LocalDateTime instance by using the static factory methods:

LocalDateTime.of(date, time); //or
LocalDate.of(date);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.