How to type in a date to a java argument
本问题已经有最佳答案,请猛点这里访问。
当我尝试在列出null的代码部分中键入日期时,它将无法工作。 它们是日期变量,但我不知道如何硬编码日期。 如果我把02/05/2018放在null的位置。 它会让我把它作为一个字符串,但我希望它作为约会进入。
就像我随机输入客户或数字一样,我想随机输入日期。
DTO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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; } |
休息
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 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; |
}
这是因为您正在键入String对象而不是Date对象。 您需要的是String to Date转换,如下所示。
1 2 3 4 5 6 7 8 | String dateString="05-02-2018"; DateFormat dateFormater = new SimpleDateFormat("dd-MM-yyyy"); Date aux; try { aux = dateFormater.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } |
然后,您可以将
如果您不想要变量创建,可以按照其他方法传递以下代码而不是
1 |