1

I want to store the time what data type should I used in spring framework ? Database which I used is 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;
    }
SFAH
  • 624
  • 15
  • 35
  • Either java.util.date or java.sql.date – fg78nc Jul 05 '17 at 05:05
  • The [@Temporal](http://docs.oracle.com/javaee/7/api/javax/persistence/Temporal.html) annotation is used to specify the TemporalType of the current annotated java.util.Date or java.util.Calendar entity attribute. – Jay Smith Jul 05 '17 at 05:05

3 Answers3

1

If you're using Java 8, the most business logic, entity fields may be using Instant - basic class in java.time API for representing a moment in the timeline in UTC.

If you need timezone data, you may consider using:

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields

Another option LocalDateTime:

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30

But then you would rely on a default system timezone which may bring contradicting results if a system timezone would change.

Check Java 8: What's the difference between Instant and LocalDateTime? or Hibernate with Java 8 LocalDate & LocalDateTime in Database for explicit explanation.

Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
0

You can use Java 8 time types in your entities. Just add this dependencies to your projects:

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

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

According to this article The 5 laws of API dates and times I'd recommend to use ZonedDateTime in REST projects.

Also pay attention to this topic...

Cepr0
  • 28,144
  • 8
  • 75
  • 101