#Time Data Type

1 messages · Page 1 of 1 (latest)

proper pond
#

what data type do you recommend using for time? i normally use instant, but i'm storing it in a database and sql Date can't convert to instant

full cedarBOT
#

<@&987246399047479336> please have a look, thanks.

proper pond
#

i'm using postgres if that matters

full cedarBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

sage plinth
jovial shoal
#

Postgres has a timestamp type.

#

Or are you looking for a LocalTime equivalent?

#
  • Java Instant <-> Postgres timestamp
  • Java LocalDate <-> Postgres date
  • Java LocalTime <-> Postgres time

The java.sql package offers Timestamp, Date and Time but they're pretty ugly to work with.

proper pond
#

yeah postgres has timestamp

#

i was using getDate

#

i didn't realize resultset has getTimestamp

#

what would be a reason to save in LocalDate instead of Instant

jovial shoal
#

scheduling something at 3:14pm every week is not an 'instant'. Similarly the date 21st April 2023 can be timezone independent. Also, the date spans all of the instants with that date in a given timezone. All of these subtleties are useful in differing scenarios.

#

An instant should be thought of as a specific record at a point in time, that is timezone independent. A ZonedDateTime would be a specific instant with timezone information, a LocalDateTime would be point in time without timezone information (so that point in time occurred/occurred at different instants for different locations).

desert jacinth
#

LocalDateTime is more accurate to what is stored than instant

drowsy kernel
#

Instant is the correct class to use within ur java code

#

u should stick to Instant as long as possible

#

and convert at the latest point necessary

#

unless u want to save date/time information that is not supposed to be placed on the timeline

#

such as an users birthday

#

that would be LocalDate

#

or an users favorite time to get up in the morning

#

thats LocalTime

#

in case u also need to remember the timezone of the user (thats rarely appropriate), u would use ZonedDateTime instead of Instant

#

here is an overview with examples of all the types:

#

#today-i-teach message