【发布时间】:2017-11-18 02:30:54
【问题描述】:
我在这个时间范围内存储在数据库中:伦敦(英国夏令时)从 15:00 到 16:00 的任何一天
如果我在此时间范围内收到事件,我需要执行程序。
我现在在巴黎 (16:22) 运行测试,而在伦敦是 15:22(所以在存储在数据库中的时间范围之间)。
这是我的代码
// create Local Date Time from what I have stored in the DB
LocalDateTime dateTime1 = LocalDateTime.of(2017, Month.JUNE, 15, 15, 00);
LocalDateTime dateTime2 = LocalDateTime.of(2017, Month.JUNE, 15, 16, 00);
Instant now = Instant.now();
System.out.println (now.isAfter (dateTime1.atZone(ZoneId.of("BST", ZoneId.SHORT_IDS)).toInstant()));
System.out.println (now.isBefore(dateTime2.atZone(ZoneId.of("BST", ZoneId.SHORT_IDS)).toInstant()));
理论上现在(巴黎的 16:22 / 伦敦的 15:22)在伦敦的 dateTime1 之后(15:00)和伦敦的 dateTime2(16:00)之前
但我知道现在不是那个 dateTime2 之前
【问题讨论】:
-
永远不要使用 3-4 个字符的缩写来表示时区。 这些是非实时时区,没有标准化,甚至不是唯一的!使用true time zone names,格式为
continent/region,例如Europe/London、Europe/Paris、Asia/Kolkata、Pacific/Auckland。
标签: java date java-8 timezone localdate