根据DST转换存储在DB中的时间

Convert time stored in DB based on DST

我在数据库中存储了时间。

ID  TYPE        STARTTIME    ENDTIME

1   WEEKDAY     08:00:00    18:00:00
2   SATURDAY    10:00:00    16:00:00
3   SUNDAY      10:00:00    16:00:00
4   HOLIDAY     10:00:00    16:00:00

Java代码:

ZonedDateTime.of(date, entity.getStartTime(), ZoneId.systemDefault());

这是在夏令时结束时检索开始时间 09:00:00 和结束时间 19:00:00。

根据夏令时检索这些时间的最佳方法是什么?

示例

对于工作日,我需要将开始时间设置为 08:00:00,将结束时间设置为 18:00:00,而不考虑 DST。

我知道这样做可能很奇怪,但您可以将其转换为 UTC,然后使用 withZoneSameLocal 将同一瞬间转换为另一个区域

This method changes the time-zone and retains the local date-time. The local date-time is only changed if it is invalid for the new zone, determined using the same approach as ofLocal(LocalDateTime, ZoneId, ZoneOffset).

ZonedDateTime dateTime = ZonedDateTime.of(LocalDate.now(),LocalTime.now() ,ZoneId.of("UTC"));

System.out.println(dateTime);  //2019-11-19T10:48:12.324356Z[UTC]

ZonedDateTime result = dateTime.withZoneSameLocal(ZoneId.of("America/Chicago"));  // provide ZoneId.systemDefault()

System.out.println(result);    //2019-11-19T10:48:12.324356-06:00[America/Chicago]