How to convert time interval from PostgreSQL in java

Metin Bulak :

I want to select the time_interval column which is of type interval from PostgreSQL.

and convert to long type with Java code

my table is

  trigger
  id   time_interval  cdate 
  1     01:00:00      2018-12-11
  2     1 day         2018-12-11
  3     1 month       2018-12-11
  4     00:05:00      2018-12-11

The Java code is

   List<Long> results= executeQuery("select * from trigger limit 10",(rs,rowNum)->{

        Long time_interval = rs.getTimestamp("time_interval").getTime();

        return time_interval ;

    });

My error is

nested exception is
org.postgresql.util.PSQLException: Bad value for type timestamp/date/time: {1}
Laurenz Albe :

There is the special type org.postgresql.util.PGInterval to handle PostgreSQL intervals:

org.postgresql.util.PGInterval i =
    (org.postgresql.util.PGInterval) rs.getObject("time_interval");

// gives something like "1 years 0 mons 0 days 2 hours 0 mins 0.00 secs"
System.out.println(i);

// gives something like "2"
System.out.println(i.getHours());

I don't know what long value you want to calculate from the interval, but if it is the length in milliseconds, you could proceed like this:

java.util.Calendar cal = new java.util.GregorianCalendar();
cal.setTimeInMillis(0);
i.add(cal);
System.out.println(cal.getTimeInMillis());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=98816&siteId=1