1

When I try to type a date in the part of the code where null is listed it wont work. They are date variables but I dont know how to just hard code a date. If i put 02/05/2018 where the null is. It will ask me to make it a string but I want it to go in as a date.

Just like how I randomly type in a customer or a number I would like to randomly type in a date.

DTO

  private Long id;
  private String name;
  private long nmcAcctNo;
  private int hubId;

  private Date createTime;

  private Date updateTime;

  public CustomerDTO()
  {

  }

  public CustomerDTO(Long id, String name, long nmcAcctNo, int hubId, Date createTime, Date updateTime)
  {
    super();
    this.id = id;
    this.name = name;
    this.nmcAcctNo = nmcAcctNo;
    this.hubId = hubId;
    this.createTime = createTime;
    this.updateTime = updateTime;
  }

REST

// Your implementation should pull the actual list of customers from the database.
List<CustomerDTO> fakeCustomerList = new ArrayList<CustomerDTO>();
fakeCustomerList.add(new CustomerDTO(1L, "Customer 1", 1, 1, null, null));
fakeCustomerList.add(new CustomerDTO(2L, "Customer 2", 2, 1, null, null));
fakeCustomerList.add(new CustomerDTO(3L, "Customer 3", 3, 1, null, null));
fakeCustomerList.add(new CustomerDTO(4L, "Customer 4", 4, 1, null, null));
fakeCustomerList.add(new CustomerDTO(5L, "Customer 5", 5, 1, null, null));
fakeCustomerList.add(new CustomerDTO(6L, "Customer 6", 6, 2, null, null));
fakeCustomerList.add(new CustomerDTO(7L, "Customer 7", 7, 2, null, null));
fakeCustomerList.add(new CustomerDTO(8L, "Customer 8", 8, 2, null, null));
fakeCustomerList.add(new CustomerDTO(9L, "Customer 9", 9, 3, null, null));
fakeCustomerList.add(new CustomerDTO(10L, "Customer 10", 10, 4, null, null));
return fakeCustomerList;

}

  • what type is storing the date in your `CustomerDTO` ? – Ousmane D. Feb 05 '18 at 22:17
  • Its a Date however if i type in a random date like i did my customer or string it will not work and I dont want to have to make a variable for it. –  Feb 05 '18 at 22:19
  • 2
    https://stackoverflow.com/questions/6510724/how-to-convert-java-string-to-date-object – Ousmane D. Feb 05 '18 at 22:21
  • No. I want to just type the date into the argument. Not create a variable. Is that not possible? –  Feb 05 '18 at 22:23
  • Instead of null try: new Date(), if you want current time(e.g, Tue Feb 06 00:32:38 2018). This way you don't create variable. – yoav Feb 05 '18 at 22:36
  • use Calendar.getInstance().getTime(); instead of null and null for createtime and updateTime. – user641887 Feb 05 '18 at 22:42
  • FYI, `Date` is a troublesome old class that is now legacy, supplanted by the *java.time* classes, in particular [`java.time.Instant`](https://docs.oracle.com/javase/9/docs/api/java/time/Instant.html). For a date-only value,use [`LocalDate`](https://docs.oracle.com/javase/9/docs/api/java/time/LocalDate.html) class: `LocalDate.of( 2018 , 2 , 5 )` for February 5th, 2018. For a date-time in your time zone , `ZonedDateTime.of( myLocalDate , myLocalTime , myZoneId )`. Search Stack Overflow as these have been covered many times already. – Basil Bourque Feb 06 '18 at 01:29

1 Answers1

1

This is because you are typing a String object and not a Date one. What you need is String to Date conversion as follows.

String dateString= "05-02-2018";
DateFormat dateFormater = new SimpleDateFormat("dd-MM-yyyy"); 
Date aux;
try {
    aux = dateFormater.parse(dateString);
} catch (ParseException e) {
    e.printStackTrace();
}

Then you can pass the aux variable where you have null.

If you don't want variable creation you can follow other approach passing the following code instead of null

new GregorianCalendar(2018, 5, 2).getTime();
Drubio
  • 1,157
  • 3
  • 13
  • 30