英文:
Setting Date Value in Controller of Spring Boot Application (Java 8)
问题
在我的模型中,我有以下内容:
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();
}
我试图按以下方式输出一个对象数组:
@RestController
public class TestingController {
@GetMapping("/saved")
public List
List
new Testing(
1,
"Text",
new Date(2000, 1, 1)),
new Testing(
2,
"Text2",
new Date(2019, 3, 27)),
我尝试使用了`new Date`,但是它不起作用,那么作为类型为`Date`的第三个参数,我需要传递什么内容呢?不是字符串或整数!
英文:
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?!?!
答案1
得分: 1
我建议您使用更新的类:
LocalDateTime
和
ZonedDateTime
然后,您可以通过使用静态工厂方法获取 LocalDateTime 实例:
LocalDateTime.of(date, time); //或者
LocalDate.of(date);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论