JSR310 when to use which classes?

60 Views Asked by At

I would like to verify if my understanding of JSR310 classes use-cases are correct, below a diagram with classes, and how i see them fit:

   Database (UTC)
       ||
       \/
  OffsetDateTime      (used in persistence or transmission, databases and XML)
       ||
       \/
    Instant            (used in business logic, for date time calculations)
       ||
       \/
   ZonedDateTime  <-- ZoneId  (used in presentation layer, requiring the client zoneId)
       ||
       \/
    LocalDateTime (used in presentation layer, obtained from ZonedDateTime)
       ||
       \/
     Front-end

My uncertainity its related with ZonedDateTime and LocalDateTime, since as far as i understand are both suitable for presentation layer, altough LocalDatetime does not have time-zone or offset.

Its the LocalDateTime what i should send to front-end, after ZonedDateTime has handled all DST conversions and anomalies?

Do i not send a ZonedDateTime to front-end since it has extra information like time-zone?

thanks in advance

1

There are 1 best solutions below

1
On

JSR310 is the Java specification for the java.time package, which provides classes for date and time manipulation in Java. The main classes in the package are:

  • LocalDate: Represents a date without a time (year, month, and day)
  • LocalTime: Represents a time without a date (hours, minutes, seconds, and nanoseconds)
  • LocalDateTime: Represents a date and time without a time zone (combines LocalDate and LocalTime)
  • ZonedDateTime: Represents a date and time with a time zone
  • Instant: Represents a point in time (similar to java.util.Date)

You should use LocalDate when you only need to represent a date, LocalTime when you only need to represent a time, LocalDateTime when you need to represent both date and time but without timezone, ZonedDateTime when you need to represent both date and time with timezone and Instant when you need to represent a point in time.