简体   繁体   English

java.sql.Time到OffsetTime

[英]java.sql.Time to OffsetTime

Hi in DB i have four columns to store a time window. 嗨,在DB中,我有四列来存储一个时间窗口。 This would allow user to store 9:00 to 5:00 EST. 这将允许用户存储9:00至5:00 EST。

Now i need to parse this information in java. 现在,我需要在Java中解析此信息。

java.sql.Time startTS = rs.getTime("begin_TIME ");
LocalTime localTime = startTS.toLocalTime();

offset could be made with: OffsetTime of(LocalTime time, ZoneOffset offset) 可以使用以下方式进行偏移:OffsetTime of(LocalTime time,ZoneOffset offset)

from what i see we cant convert zoneid to zoneoffset, so how do i 9:00 est(stored in time and zone columns) from sqlserver to java. 从我所看到的,我们不能将zoneid转换为zoneoffset,所以我如何将9:00 est(存储在时间和区域列中)从sqlserver转换为java。

DB Table:
 begin_TIME time NOT NULL,
 begin_TIME_ZONE varchar(5) NOT NULL,
 end_TIME time NOT NULL,
 end_TIME_ZONE varchar(5) NOT NULL,

In the back-end I need to check that the request is in the window, request time is converted to ZonedDateTime and start and end need to come from DB: 在后端,我需要检查请求是否在窗口中,请求时间已转换为ZonedDateTime,并且开始和结束需要来自数据库:

public boolean compare(ZonedDateTime dateTime, OffsetTime startTime, OffsetTime endTime) {
    OffsetTime offsetTime = dateTime.toOffsetDateTime().toOffsetTime();
    int start = offsetTime.compareTo(startTime);
    int end = offsetTime.compareTo(endTime);
    return start >= 0 && end <= 0;
}

If I understand your situation right, this variant of your compare method should be able to help you: 如果我正确理解您的情况,那么您的compare方法的这种变体应该可以为您提供帮助:

public boolean compare(ZonedDateTime dateTime, LocalTime start, ZoneId startTz,
        LocalTime end, ZoneId endTz) {
    ZonedDateTime startZonedDateTime = start.atDate(dateTime.toLocalDate()).atZone(startTz);
    ZonedDateTime endZonedDateTime = end.atDate(dateTime.toLocalDate()).atZone(endTz);
    return (! dateTime.isBefore(startZonedDateTime)) && (! dateTime.isAfter(endZonedDateTime));
}

I am using the date part of the supplied ZonedDateTime to determine whether the comparison is to be made on a date where daylight saving time is in effect. 我正在使用提供的ZonedDateTime的日期部分来确定是否在生效夏令时的日期进行比较。 I chose so because Java needs a date to be able to convert a local time to a zoned or offset time when the time zone may have daylight savings time. 我之所以这样选择,是因为Java需要一个日期才能将本地时间转换为时区可能具有夏令时的时间。

It is possible to compare OffsetTime (ignoring dates) objects as you do in your version, but I found it somewhat simpler to compare ZonedDateTime objects instead. 可以像在版本中那样比较OffsetTime (忽略日期)对象,但是我发现比较ZonedDateTime对象稍微简单一些。 What it really does is compare the corresponding instants, so it works well even though the time zones differ. 它实际上所做的是比较相应的瞬间,因此即使时区不同,它也可以正常工作。

I trust you to supply the two zone ID objects to the method. 我相信您可以向该方法提供两个区域ID对象。 If you end up storing time zones as America/Toronto in your database, this can be fed directly into ZoneId.of() . 如果最终在America/Toronto数据库中将时区存储为America/Toronto ,则可以将其直接输入ZoneId.of()

I really don't think you should stick to EST and PST . 我真的不认为您应该坚持ESTPST It's getting too complicated and errorprone. 它变得太复杂且容易出错。 Java can translate EST to -05:00 ; Java可以将EST转换为-05:00 ; this leaves you with no adjustment for daylight savings time (it does understand PST as America/Los_Angeles though). 这使您无需调整夏令时(尽管它确实将PST理解为America/Los_Angeles )。 Use the modern forms America/Toronto etc. See the answers to this question: How to tackle daylight savings using Timezone in java . 使用现代形式的America/Toronto等。请参见以下问题的答案:如何使用Java中的Timezone解决夏时制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM