如何将间隔类型格式化为HH:MM格式? [英] How to format Interval type to HH:MM format?

查看:73
本文介绍了如何将间隔类型格式化为HH:MM格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的列中使用oracle DB

I am using oracle DB with below column

START_TIME INTERVAL DAY(0) TO SECOND(0)

我正在使用Java代码中的jdbc模板通过行映射器和getString()方法访问此值,如下所示:(我正在运行基本的select查询以从DB中获取值)

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

我无法使用 HH:MM 格式字符串设置此值的格式.我不需要秒,因为我忽略了该值.所以我想要的是这样的 09:30

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

发现了类似的东西,但我正在大张旗鼓地使用用于DTO的基于API yaml的生成,不知道该如何实现.另外,我没有使用休眠模式.这是普通的JDBC模板.

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.

观察到,当我在数据库中有+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:00,并且09:30.希望这会有所帮助.

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.

推荐答案

我相信操纵字符串值是最简单的解决方案,所以我将提出一个替代解决方案.

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

从Oracle文档中的

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.

从Oracle

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.)

javadoc oracle.sql.INTERVALDS:

该对象的内部数据作为11字节数组存储在超类的存储区域中.字节排列如下:

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

由于列START_TIME的定义,即零天零小数秒,您知道只有字节4、5和6是相关的.但是由于您在问题中写道,您忽略了秒,所以这意味着仅字节4和5是相关的.因此,用于从ResultSet检索值并将其转换为所需格式的字符串的代码为:

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);

这篇关于如何将间隔类型格式化为HH:MM格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆