【问题标题】:Setting Date Value in Controller of Spring Boot Application (Java 8) [duplicate]在 Spring Boot 应用程序(Java 8)的控制器中设置日期值 [重复]
【发布时间】:2023-04-03 11:08:02
【问题描述】:

在我的模型中,我有以下内容:

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 但这不起作用,那么我究竟需要将什么作为第三个参数作为类型 Date 而不是 String 或 int 传递?!?!

【问题讨论】:

  • Date 来自什么包?是java.util.Date吗?
  • @Nikolas 是的...
  • 当您使用 Java 8 时,请不要使用 20 岁的 已弃用 java.util.Date。请改用java.time.LocalDate
  • 您的问题不清楚。请分享你得到了什么例外?
  • 考虑使用LocalDateLocalDateTime 而不是java-8

标签: java spring-boot api gradle java-8


【解决方案1】:

我建议你使用较新的类:

LocalDateTime

ZonedDateTime

然后你可以使用静态工厂方法获取一个 LocalDateTime 实例:

LocalDateTime.of(date, time); //or
LocalDate.of(date);

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2018-03-25
    • 2013-03-25
    • 2014-07-30
    • 2019-01-07
    相关资源
    最近更新 更多