如何处理存储在 MySQL TIME 字段中的时间持续时间?

发布于 2025-01-17 06:14:49 字数 491 浏览 3 评论 0 原文

我遇到一个问题,我的 MySQL 表有一个存储 UTC 时间偏移的字段。该字段的类型为 TIME。这是出于遗留原因,我无法将列类型更改为更好的表示形式。然而 MySQL 支持这一点。它允许用户在时间字段中存储负值来表示负时间差。我使用 querydsl 生成实体类,并将该字段映射到 java.sql.Time 对象。现在,当数据库中有负值时,我面临一个问题。 java.sql.Time 类将任何负值转换为前一天的时间。例如,如果 DB 中的值为 -04:00:00,则 Java 对象的时间为 20:00:00。我正在考虑以某种方式将其转换为持续时间的选项,但后来遇到了 UTC 时区偏移在某些区域重叠的问题。

此处,负偏移量下降到 -12:00:00,正偏移量上升到 +14:00:00。现在的问题是我无法区分 Java 对象中 -12:00 和 +12 的值之间的差异,因为它们在 java 对象中都设置为 12:00:00。 +12:00:00 到 +14:00:00 两侧的值也有重叠。

任何处理这种情况的建议都将受到高度赞赏。

I ran into an issue where my MySQL Table has a field that stores a UTC Time Offset. The field is of the type TIME. This is for Legacy reasons and I cannot change the column type to a better representation. MySQL, however, supports this. It allows the user to store a negative value in a time field to represent a negative time difference. I use querydsl for the Entity class generation and it mapped this field to a java.sql.Time object. Now I am facing an issue when there is a negative value in the DB. The java.sql.Time class converts any negative value into the time in the previous day. For example, if the value in the DB is -04:00:00, the Java object has the time 20:00:00. I was considering an option of converting this somehow to a Duration but then I ran into the issue where UTC timezone offsets overlap in certain regions.

Here, the negative offset goes down to -12:00:00 and the positive side goes up to +14:00:00. Now the problem is that I can't tell the difference between the values in the Java object for -12:00 and +12 because they both set to 12:00:00 in the java object. +12:00:00 to +14:00:00 also has overlapping values from both sides.

Any suggestion to handle this situation is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2025-01-24 06:14:49

我无法帮助 QueryDSL。

java.time

java.time中,代表一个与 UTC 的偏移量java.time.ZoneOffset 类。

您可以尝试这两种方法中的任何一种,看看它们是否有效。

将 MySQL TIME 文本解析为 java.time.ZoneOffset

也许你可以找回不幸的 MySQL TIME 值作为文本。然后解析为 ZoneOffset 对象。

String o = myResultSet.getString( … ) ;  // "-12:00:00" or "14:00:00", and such.
ZoneOffset zoneOffset = ZoneOffset.of( o ) ;

Retrieve as ZoneOffset

我不知道这是否有效,但您可以尝试直接将 MySQL TIME 提取为 ZoneOffset。指定所需的 Java 类作为参数。

ZoneOffset zoneOffset = myResultSet.getObject( … , ZoneOffset.class ) ; 

I cannot help with QueryDSL.

java.time

In java.time, represent an offset-from-UTC with java.time.ZoneOffset class.

You might try either of these two approaches to see if they work.

Parse MySQL TIME textually as java.time.ZoneOffset

Perhaps you could retrieve your unfortunate MySQL TIME values as text. Then parse as ZoneOffset objects.

String o = myResultSet.getString( … ) ;  // "-12:00:00" or "14:00:00", and such.
ZoneOffset zoneOffset = ZoneOffset.of( o ) ;

Retrieve as ZoneOffset

I have no idea if this works, but you could try extracting the MySQL TIME as a ZoneOffset directly. Specify the desired Java class as an argument.

ZoneOffset zoneOffset = myResultSet.getObject( … , ZoneOffset.class ) ; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文