java - 在 Spring Boot 应用程序的控制器中设置日期值(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<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)),
我尝试使用 new Date 但这不起作用,那么我到底需要将什么作为第三个参数作为 typeDate
而不是 String 或 int 传递?!?!
解决方案
我建议您使用较新的类:
LocalDateTime
和
ZonedDateTime
然后您可以使用静态工厂方法获取 LocalDateTime 实例:
LocalDateTime.of(date, time); //or
LocalDate.of(date);
推荐阅读
- ruby-on-rails - 如何在 ruby on rails 中设置像 amp-email 这样的动态电子邮件?
- javascript - 在javascript中实现快速排序算法的比较计数器
- java - 如何删除我的代码中的重复整数?
- swift - 我应该如何在相同的操作中设置按钮的图像?
- php - PHP Slim 3 API 中间件类不能来自 __invoke 的方法?
- mysql - 相同记录集上的相同 UPDATE SQL 似乎与 MariaDB 行为不端
- node.js - google sheet api v4 在特定行之后附加行或将行移动到特定位置
- python - 如何将注释箭头添加到图例
- haskell - 我可以用“递归方案”“cata”来写“foldr”(或“foldMap”)吗?
- sql - SQL Server 比较 varbinary 列