提问者:小点点

Spring数据Rest中的时间数据类型


我想存储时间,我应该在Spring框架中使用什么数据类型?我使用的数据库是MySQL

@Entity
public class ShopDetail {

@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String address;

    private Double latitude;

    private Double longitude;

    private float  rating;

    private Time openingTime;

    private Time closingTime;
    }

共3个答案

匿名用户

如果您使用的是 Java 8,这是大多数业务逻辑,则实体字段可能使用 java.time API 中的即时基本类来表示 UTC 时间轴中的时刻。

如果您需要时区数据,您可以考虑使用:

ZonedDateTime是带有时区的日期时间的不可变表示。此类存储所有日期和时间字段

另一个选项LocalDateTime:

ISO-8601 日历系统中没有时区的日期时间,例如 2007-12-03T10:15:30

但是,如果系统时区发生变化,您将依赖默认的系统时区,这可能会带来矛盾的结果。

查Java 8:Instant和LocalDateTime有什么区别?或者用Java 8 LocalDateHibernate

匿名用户

您可以使用日期时间格式。

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html

Spring 3.2日期时间格式

匿名用户

您可以在实体中使用 Java 8 时间类型。只需将此依赖项添加到您的项目中:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
</dependency>

根据这篇文章,我建议在REST项目中使用ZonedDateTime的API日期和时间的5条定律。

还要注意这个话题……