I'm using Freemarker Version 2.3.20.
I have a data structure where two dates are contained - one in local time and one in utc time.
// 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 can handle only java.util.Date
so I'm handing over dates.
Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("departureTimeLocal", Date.from(localTime.toInstant()));
mapping.put("departureTimeUtc", Date.from(utcTime.toInstant()));
In my template I would expect to write something like:
Departure (local): ${departureTimeLocal?string['HH:mm']}
Departure (UTC) : ${departureTimeUtc?string['HH:mm']}
And as a result I would like to see:
Departure (local): 20:30
Departure (UTC) : 18:30
What I see currently is:
Departure (local): 20:30
Departure (UTC) : 20:30 <#-- timestamp interpreted in local time -->
I've also tried something like:
Departure (converted): ${(departureTimeLocal?string['yyyy-MM-dd HH:mm'] + ' UTC')?datetime['yyyy-MM-dd HH:mm z']?string['HH:mm']}
--> Departure (converted): 22:30
What would be the best way to archive something like that?
Yes I know: java.util.Date
does not really have a timezone (only for printing) and localTime/utcTime.toInstant()
both map to the same instants in zulu time.
eval. This built-in evaluates a string as an FTL expression. For example "1+2"? eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)
FreeMarker doesn't support modifying collections. But if you really want to do this in FreeMarker (as opposed to in Java), you can use sequence concatenation: <#assign myList = myList + [newItem]> . Here you create a new sequence that wraps the two other sequences.
FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation.
FreeMarker is a server-side Java template engine for both web and standalone environments. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language. Note: Spring Boot recently changed the default extension from . ftl to . ftlh.
With freemarker 2.3.20, you can use the iso date built-in:
${departureTimeUtc?time?iso_utc_m_nz}
This built-in is deprecated since freemarker 2.3.21, and replaced by:
${departureTimeUtc?time?string.iso_m_nz_u}
The meaning of iso_m_nz_u
is:
iso
: use ISO 8601:2004 format m
: accuracy to the minutenz
: no timezone u
: use UTC instead of default timezoneA complete list of options can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With