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 7092937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:22:36+00:00 2026-05-28T08:22:36+00:00

Java date & time datatypes are as everybody knows, there’s no need to focus

  • 0

Java date & time datatypes are as everybody knows, there’s no need to focus on that.

However, when I’m using JDBC or Spring-based extensions, such as SimpleJdbcTemplate to retrieve and store interval values, what Java type should I use, if I don’t want to use org.postgresql.util.PGInterval class?

This class is internal of PostgreSQL driver so using it would make the code DB-specific. I think it should be possible to operate on intervals in DB-agnostic way, because it is one of standard SQL types.

  • 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-28T08:22:37+00:00Added an answer on May 28, 2026 at 8:22 am

    An interval isn’t one of the standard JDBC Types, as listed in the java.sql.Types class. I know that if you call resultSet.getObject("interval_column"), it is a PGInterval when casted, so it looks like the PG JDBC driver might be forcing your hand to deal with it as such, unless you do what Glenn says and convert it to a string, or maybe a number, in your SQL.

    In our application, we use JodaTime for all of our date management, and we have a Hibernate Type written that converts our bean property to and from a PGInterval, and use getObject and setObject to communicate with JDBC. I doubt that code would help you deal with what you are looking for here, but I can share it with you if you are interested.

    UPDATED: Here is the Hibernate Type class that converts between Joda Time and PGInterval. I know this doesn’t answer the question, but the original poster asked for the sample code.

    package com.your.package.hibernate.types;
    
    import java.io.Serializable;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    
    import org.hibernate.HibernateException;
    import org.hibernate.usertype.UserType;
    import org.joda.time.DurationFieldType;
    import org.joda.time.Period;
    import org.joda.time.ReadableDuration;
    import org.joda.time.ReadablePeriod;
    import org.postgresql.util.PGInterval;
    
    public class JodaTimeDurationType
        implements UserType {
    
        public Class<?> returnedClass() {
            return ReadableDuration.class;
        }
    
    
        public int[] sqlTypes() {
            return new int[] {Types.OTHER};
        }
    
    
        public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
            throws HibernateException, SQLException {
    
            try {
                final PGInterval pgi = (PGInterval)resultSet.getObject(names[0]);
    
                final int years = pgi.getYears();
                final int months = pgi.getMonths();
                final int days = pgi.getDays();
                final int hours = pgi.getHours();
                final int mins = pgi.getMinutes();
                final double secs = pgi.getSeconds();
    
                return new Period(years, months, 0, days, hours, mins, (int)secs, 0).toStandardDuration();
    
            }
            catch (Exception e) {
                return null;
            }
        }
    
    
        public void nullSafeSet(PreparedStatement statement, Object value, int index)
            throws HibernateException, SQLException {
    
            if (value == null) {
                statement.setNull(index, Types.OTHER);
            }
            else {
                final ReadablePeriod period = ((ReadableDuration)value).toPeriod();
    
                final int years = period.get(DurationFieldType.years());
                final int months = period.get(DurationFieldType.months());
                final int days = period.get(DurationFieldType.days());
                final int hours = period.get(DurationFieldType.hours());
                final int mins = period.get(DurationFieldType.minutes());
                final int secs = period.get(DurationFieldType.seconds());
    
                final PGInterval pgi = new PGInterval(years, months, days, hours, mins, secs);
                statement.setObject(index, pgi);
            }
        }
    
    
        public boolean equals(Object x, Object y)
            throws HibernateException {
    
            return x == y;
        }
    
    
        public int hashCode(Object x)
            throws HibernateException {
            return x.hashCode();
        }
    
    
        public Object deepCopy(Object value)
            throws HibernateException {
            return value;
        }
    
    
        public boolean isMutable() {
            return false;
        }
    
    
        public Serializable disassemble(Object value)
            throws HibernateException {
            throw new HibernateException("not implemented");
        }
    
    
        public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
            throw new HibernateException("not implemented");
        }
    
    
        public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
            throw new HibernateException("not implemented");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using android.text.format.DateFormat.format(yyyy-MM-dd hh:mm:ss, new java.util.Date()) to get time & date. The problem
There is a Java service that I need to invoke from PHP that looks
I'm writing a JSP that sometimes needs to format a Java Date that comes
I have an application that passes in java.util.Date. I want to check whether this
What is really meant when using Java Date utilities and something has been deprecated.
I want to get the difference between two Java Date objects. I've used Joda-Time
I need html - java script / jquery charts using single file. What do
I have a form with a field that uses a date/time picker which produces
I'm working on development of a social web application using Java. I need to
I have been trying to add to the Watch window a Java Date object

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.