我的时间存储在数据库中。
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。
最佳答案
我知道这样做可能很奇怪,但您可以将其转换为 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]
关于java - 根据 DST 转换存储在 DB 中的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938899/