如何将oracle的interval数据类型格式化为hh:mm格式?

hujrc8aj  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(355)

我在oracledb中有一个表,下面有一列

START_TIME INTERVAL DAY(0) TO SECOND(0)

我正在运行一个基本的select查询来从db获取值。我使用的是jdbc模板和getstring(),如下所示

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

当我有 +00 11:00:00.000000 在数据库中 rs.getString("START_TIME") 获取 0 11:0:0.0 什么时候 +00 09:30:00.000000 它吸引人 0 9:30:0.0 但我的要求是11点和9点半
有人能帮我格式化吗?
我已经尝试了以下方法:
如何在java中表示oracle interval—但是我正在使用基于swagger open api yaml的DTO生成,不知道如何实现这一点。另外,我没有使用hibernate。它是普通的jdbc模板。
我以前的问题-但对我没有帮助

im9ewurl

im9ewurl1#

您可以尝试以下变体之一:

// add this function: it converts intervalDS to Time/Timestamp
    public static Time intervalDStoTime(INTERVALDS intervalds) throws ParseException {
        SimpleDateFormat intervalFormat = new SimpleDateFormat("dd hh:mm:ss.SSS");
        java.util.Date parsedDate = intervalFormat.parse(intervalds.stringValue());
        Time time = new java.sql.Time(parsedDate.getTime());
        // or you can replace previous line with this one if yu will need timestamp:
        // Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
        return time;
    }

//...
// your code:
    //Original:
    oracle.sql.INTERVALDS startTime = rs.getINTERVALDS("START_TIME");
    System.out.println("Original:" + startTime);
    // 1: interval to time:
    Time time = intervalDStoTime(startTime);
    System.out.println("ToTime: " + time);
    // 2: + format
    System.out.println("ToTime + format: " + new SimpleDateFormat("hh:mm").format(time));
    // 3: format bytes
    byte[] bytes = startTime.toBytes();
    int hour = bytes[4] - 60;
    int minute = bytes[5] - 60;
    String hhmm = String.format("%02d:%02d", hour, minute);
    System.out.println("toBytes + format: " + hhmm);

相关问题