Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:01:08+00:00 2026-05-23T14:01:08+00:00

I’m storing two dates in the PostgreSQL database. First, is the data of visit

  • 0

I’m storing two dates in the PostgreSQL database. First, is the data of visit of a webpage, and the second date is the date of last modification of the webpage(this is get as a long).

I have some doubts what is the best strategy to store these values.

I only need day/month/year and hour:seconds and this will only for statistical proposes.

So, some doubts:

  • is best store as long and convert on recover of information or store in the data format above?
  • is best set the date of visit on the software or in the insertion in the database?
  • in Java, how are the best classes to handle dates?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:01:08+00:00Added an answer on May 23, 2026 at 2:01 pm

    Any strategy for storing date-and-time data in PostgreSQL should, IMO, rely on these two points:

    • Your solution should never depend on the server or client timezone setting.
    • Currently, PostgreSQL (as most databases) doesn’t have a datatype to store a full date-and-time with timezone. So, you need to decide between an Instant or a LocalDateTime datatype.

    My recipe follows.


    If you want to record the physical instant at when a particular event ocurred, (a true "timestamp" , typically some creation/modification/deletion event), then use:

    • Java: Instant (Java 8 , or Jodatime).
    • JDBC: java.sql.Timestamp
    • PostgreSQL: TIMESTAMP WITH TIMEZONE (TIMESTAMPTZ)

    (Don’t let PostgreSQL peculiar datatypes WITH TIMEZONE/WITHOUT TIMEZONE confuse you: none of them actually stores a timezone)

    Some boilerplate code: the following assumes that ps is a PreparedStatement, rs a ResultSet and tzUTC is a static Calendar object corresponding to UTC timezone.

    public static final Calendar tzUTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));  
    

    Write Instant to database TIMESTAMPTZ:

    Instant instant = ...;
    Timestamp ts = instant != null ? Timestamp.from(instant) : null;
    ps.setTimestamp(col, ts, tzUTC);   // column is TIMESTAMPTZ!
    

    Read Instant from database TIMESTAMPTZ:

    Timestamp ts = rs.getTimestamp(col,tzUTC); // column is TIMESTAMPTZ
    Instant inst = ts !=null ? ts.toInstant() : null;
    

    This works safely if your PG type is TIMESTAMPTZ (In that case, the calendarUTC has no effect in that code ; but it’s always advisable to not depend on defaults timezones).
    "Safely" means that the result will not depend on server or database timezone, or timezones information: the operation is fully reversible, and whatever happens to timezones settings, you’ll always get the same "instant of time" you originally had on the Java side.


    If, instead of a timestamp (an instant on the physical timeline), you are dealing with a "civil" local date-time (that is, the set of fields {year-month-day hour:min:sec(:msecs)}), you’d use:

    • Java: LocalDateTime (Java 8 , or Jodatime).
    • JDBC: java.sql.Timestamp
    • PostgreSQL: TIMESTAMP WITHOUT TIMEZONE (TIMESTAMP)

    Read LocalDateTime from database TIMESTAMP:

    Timestamp ts = rs.getTimestamp(col, tzUTC); //
    LocalDateTime localDt = null;
    if( ts != null ) 
        localDt =  LocalDateTime.ofInstant(Instant.ofEpochMilli(ts.getTime()), ZoneOffset.UTC);
    

    Write LocalDateTime to database TIMESTAMP:

      Timestamp ts = null;
      if( localDt != null)    
          ts = new Timestamp(localDt.toInstant(ZoneOffset.UTC).toEpochMilli()), tzUTC);
      ps.setTimestamp(colNum,ts, tzUTC); 
    

    Again, this strategy is safe and you can sleep peacefully: if you stored 2011-10-30 23:59:30 , you’ll retrieve those precise fields (hour=23, minute=59… etc) always, no matter what – even if tomorrow the timezone of your Postgresql server (or client) changes, or your JVM or your OS timezone, or if your country modifies its DST rules, etc.


    Added: If you want (it seems a natural requirement) to store the full datetime specification (a ZonedDatetime: the timestamp together with the timezone, which implicitly also includes the full civil datetime info – plus the timezone)… then I have bad news for you: PostgreSQL hasn’t a datatype for this (neither other databases, to my knowledge). You must devise your own storage, perhaps in a pair of fields: could be the two above types (highly redundant, though efficient for retrieval and calculation), or one of them plus the time offset (you lose the timezone info, some calculations become difficult, and some impossible), or one of them plus the timezone (as string; some calculations can be extremely costly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.