我正在使用Freemarker版本 2.3.20 。
我有一个数据结构,其中包含两个日期 - 一个在本地时间,另一个在 utc时间。
// 2017-07-17 18:30 UTC
ZonedDateTime utcTime = ZonedDateTime.of(2017, 7, 17, 18, 30, 0, 0, ZoneId.of("UTC"));
// 2017-07-17 20:30 (+02:00)
ZonedDateTime localTime = utcTime.withZoneSameInstant(ZoneId.of("Europe/Berlin"));
Freemarker只能处理java.util.Date
所以我要交出日期。
Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("departureTimeLocal", Date.from(localTime.toInstant()));
mapping.put("departureTimeUtc", Date.from(utcTime.toInstant()));
在我的模板中,我希望写出类似的内容:
Departure (local): ${departureTimeLocal?string['HH:mm']}
Departure (UTC) : ${departureTimeUtc?string['HH:mm']}
结果我希望看到:
Departure (local): 20:30
Departure (UTC) : 18:30
我目前看到的是:
Departure (local): 20:30
Departure (UTC) : 20:30 <#-- timestamp interpreted in local time -->
我也尝试过类似的事情:
Departure (converted): ${(departureTimeLocal?string['yyyy-MM-dd HH:mm'] + ' UTC')?datetime['yyyy-MM-dd HH:mm z']?string['HH:mm']}
--> Departure (converted): 22:30
归档类似内容的最佳方式是什么?
是的我知道:java.util.Date
实际上没有时区(仅用于打印),而localTime/utcTime.toInstant()
在zulu时间都映射到相同的时刻。