1

I am confused if I can use Instant, LocalDate, LocalDateTime etc. as a data type for my date variable in entity class? Is it possible or should I use Date in Entity classes and when I do some logic I should convert the Date to some of the types mentioned above?

Thank you!

Bashir
  • 2,057
  • 5
  • 19
  • 44
vviston
  • 183
  • 1
  • 12
  • 5
    It depends on what info of date you want to store – Eklavya Jun 10 '20 at 14:59
  • 4
    I understand your confusion. You need to pick according to the need in your specific situation, there is no one and only date/time type to use. Each has its place (except `Date`, it’s poorly designed and long outdated, it hasn’t got any place anymore). – Ole V.V. Jun 10 '20 at 14:59
  • And Type names are quite meaningful to understand – Eklavya Jun 10 '20 at 15:04
  • Read the javadocs for each. Some represent just date (25th May 2020), some represent date + time (3PM 25th May 2020), some represent date + time + the timezone of the place that date/time applies to, etc (3PM 25th May 2020, GMT) – Michael Jun 10 '20 at 15:04
  • If you mean how to save `Date` in your entity class, maybe you can use UTC timestamp in long or UTC string in format like `1994-11-05T13:15:30Z` And then you can convert this UTC time into any type, for example LocalDate based on your current timezone or ZonedDateTime for a specific timezone. – LiuWenbin_NO. Jun 10 '20 at 15:05
  • 1
    Did you search for similar questions? Here’s one, for example: [Which date class should I use in Java 8?](https://stackoverflow.com/questions/35301269/which-date-class-should-i-use-in-java-8) There are more. – Ole V.V. Jun 10 '20 at 15:14
  • The graphic in [this answer by basil Bourque](https://stackoverflow.com/a/62273365/5772882) could prove quite helpful. – Ole V.V. Jun 10 '20 at 15:27

0 Answers0