How to format Interval type to HH:MM format?

浪尽此生 提交于 2021-02-11 16:29:10

问题


I am using oracle DB with below column

START_TIME INTERVAL DAY(0) TO SECOND(0)

I am using jdbc template in java code to access this value using the row mapper and getString() method like below: (I am running a basic select query to fetch values from DB)

String startTime = rs.getString("START_TIME");

and the value I get is in this format

System.out.println(startTime); // 0 9:30:0.0

I am not able to format this value in the HH:MM format string. I do not need the seconds as I am ignoring that value. So what I want is something like this 09:30

Found this which looks similar but I am using swagger open API yaml based generation for DTOs and don't know how can I achieve this. Also, I am not using hibernate. It's plain JDBC Template.

EDIT: Observed that when I have +00 11:00:00.000000 in the DB, the rs.getString("START_TIME") fetches 0 11:0:0.0 and when +00 09:30:00.000000 it fetches 0 9:30:0.0 but my requirement is 11:00 and 09:30. Hope this helps a little more.


回答1:


I believe that manipulating the string value is the easiest and simplest solution, so I will present an alternative solution.

From the Oracle documentation for the INTERVAL DAY TO SECOND data type and from the definition of column START_TIME in your question, the column values cannot span more than one day nor can they contain fractional seconds.

From the Oracle JDBC documentation, the datatype INTERVAL DAY TO SECOND maps to the java class oracle.sql.INTERVALDS. (This is one of the classes in the Oracle JDBC driver JAR file.)

From the javadoc of class oracle.sql.INTERVALDS:

The internal data for this object is stored as a 11 byte array in the super class' storage area. The bytes are arranged as follows:

 Byte       Represents
   0         High byte of day
   1         2nd high byte of day
   2         3rd high byte of day
   3         least byte of day   
   4         hour val + 60     
   5         min + 60
   6         sec + 60
   7         High byte of Fractional second
   8         2nd high byte of Fractional Second
   9         3rd high byte of Fractional Second
   10        least byte of Fractional Second

You know that only bytes 4, 5 and 6 are relevant because of the definition of column START_TIME, i.e. zero days and zero fractional seconds. But since you wrote in your question that you are ignoring the seconds, that means only bytes 4 and 5 are relevant. Hence the code for retrieving the value from the ResultSet and converting it to a string in your desired format is:

INTERVALDS intervalDS = (INTERVALDS) rs.getObject("START_TIME");
byte[] bytes = intervalDS.toBytes();
int hour = bytes[4] - 60;
int minute = bytes[5] - 60;
String result = String.format("%02d:%02d", hour, minute);



回答2:


Find the substring from after the first space character to the second : colon character and left-pad it with a zero so that it is 5 characters long.

public static String getHoursMinutesFromInterval( final String time )
{
  String hhmm = time.replaceAll( "^0 (\\d{1,2}:\\d{2}):\\d+\\.\\d+$", "0$1" );
  if ( hhmm.length() > 5 )
  {
    hhmm = hhmm.substring(1, 6);
  }
  return hhmm;
}


来源:https://stackoverflow.com/questions/62084887/how-to-format-interval-type-to-hhmm-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!