如何将时间间隔类型格式化为 HH:MM 格式?

huangapple go评论85阅读模式
英文:

How to format Interval type to HH:MM format?

问题

我正在使用Oracle数据库,并且有以下列

`START_TIME	INTERVAL DAY(0) TO SECOND(0)`

我在Java代码中使用jdbc模板来访问此值,使用行映射器和getString()方法,如下所示:(我正在运行基本的选择查询来从数据库中获取值)

    String startTime = rs.getString("START_TIME");
而我得到的值的格式如下

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

我无法将此值格式化为**HH:MM**的字符串格式。我不需要秒,因为我忽略了那个值。所以我想要的是像这样的格式 **09:30**

[找到了一个看起来类似的解决方法][1],但我是使用swagger open API基于yaml生成DTO,不知道如何实现。另外,我没有使用Hibernate,而是纯粹的JDBC模板。

  [1]: https://stackoverflow.com/questions/25236798/how-to-represent-oracle-interval-in-java

**编辑:**观察到当我在数据库中有 `+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`。希望这能再多帮助一点。
英文:

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

得分: 2

我认为操作字符串值是最简单和最直接的解决方案,因此我将提供一种替代方案。

根据Oracle关于INTERVAL DAY TO SECOND数据类型的文档和您问题中START_TIME列的定义,该列的值不能跨越超过一天,也不能包含小数秒。

根据Oracle的JDBC文档,数据类型INTERVAL DAY TO SECOND映射到Java类oracle.sql.INTERVALDS。(这是Oracle JDBC驱动程序JAR文件中的类之一。)

根据oracle.sql.INTERVALDS类的javadoc

> 此对象的内部数据存储为超类存储区中的11字节数组。字节的排列如下:

> 字节 表示
0 天的高字节
1 天的次高字节
2 天的第三个高字节
3 天的最低字节
4 小时值 + 60
5 分钟 + 60
6 秒 + 60
7 小数秒的高字节
8 小数秒的次高字节
9 小数秒的第三个高字节
10 小数秒的最低字节

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

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);
英文:

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

得分: 0

从第一个空格字符后到第二个冒号:字符之间找到子字符串,并在左侧填充零,使其长度为5个字符。

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;
}
英文:

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

huangapple
  • 本文由 发表于 2020年5月29日 19:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62084887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定