This is a tiny Java library that adds basic support for the new Java 8 date/time api to FreeMarker. Its not a perfect solution as we cannot add built-ins to FreeMarker from external code. Hopefully FreeMarker will add native support in the future, but there are no implementation being worked on at the moment (http://freemarker.org/contribute.html).
The library has basic formatting support for all classes in the java.time api introduced in Java 8, using the new java.time.format.DateTimeFormatter. We have also introduced som comparison methods.
Take a look at our feature test spec too see how it all works : https://github.com/amedia/freemarker-java-8/blob/master/src/test/resources/no/api/freemarker/java8/time/
Java 8 or higher. Tested on Freemarker 2.3.23, but should at least work fine for all 2.3.x versions. Please file an issue if you have problems with other versions.
freemarker-java-8 is deployed to the Maven Central Repository. You can include the package in your Maven POM like this :
<dependency>
<groupId>no.api.freemarker</groupId>
<artifactId>freemarker-java8</artifactId>
<version>1.1.5</version>
</dependency>
Make sure to replace the version with the current version found in the pom.
You need to configure FreeMarker with our package by adding the Java8ObjectWrapper
.
this.configuration = new Configuration(); // Or get the configuration from your framework like DropWizard or Spring Boot.
this.configuration.setObjectWrapper(new Java8ObjectWrapper(Configuration.VERSION_2_3_23));
Within a Spring project you can add this configuration class to your project:
import no.api.freemarker.java8.Java8ObjectWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class FreemarkerConfig extends FreeMarkerAutoConfiguration.FreeMarkerWebConfiguration {
@Autowired
private freemarker.template.Configuration configuration;
@PostConstruct
public void postConstruct() {
configuration.setObjectWrapper(
new Java8ObjectWrapper(freemarker.template.Configuration.getVersion())); // VERSION_2_3_26
}
}
We had to cheat a little bit to add format methods to our date.time classes. This is why you will see that our syntax differs from the default FreeMarker built-ins.
All format methods uses the java.time.format.DateTimeFormatter for formatting.
java.time class | methods | comment | example |
---|---|---|---|
Clock |
|
This is a simple implementation where format just prints the toString() value of the object. |
|
Duration |
|
Gives access to the Duration values |
|
Instant |
|
This is a simple implementation where format just prints the toString() value of the object. |
|
LocalDate |
|
Allows you to print a LocalDate on a default pattern, by providing a custom pattern or a builtin format style. |
|
LocalDateTime |
|
Allows you to print a LocalDateTime on a default pattern, by providing a custom pattern or a builtin format style. |
|
LocalTime |
|
Allows you to print a LocalTime on a default pattern, by providing a custom pattern or a builtin format style. |
|
MonthDay |
|
Allows you to print a MonthDay on a default pattern or by providing a custom pattern. |
|
OffsetDateTime |
|
Allows you to print a OffsetDateTime on a default pattern, by providing a custom pattern or a builtin format style. |
|
OffsetTime |
|
Allows you to print a OffsetTime on a default pattern, by providing a custom pattern or a builtin format style. |
|
Period |
|
Gives access to the values of the Period object. |
|
Year |
|
Allows you to print a Year on a default pattern or by providing a custom pattern. |
|
YearMonth |
|
Allows you to print a YearMonth on a default pattern or by providing a custom pattern. |
|
ZonedDateTime |
|
Allows you to print a YearMonth on a default pattern/timezone or by providing a custom pattern. |
|
ZoneId |
|
Prints the ZoneId display name. You can override the textstyle with one of these values [FULL, FULL_STANDALONE, SHORT, SHORT_STANDALONE, NARROW and NARROW_STANDALONE]. You can also override the locale, but Java only seems to have locale support for a few languages. |
|
ZoneOffset |
|
Prints the ZoneOffset display name. You can override the textstyle with one of these values [FULL, FULL_STANDALONE, SHORT, SHORT_STANDALONE, NARROW and NARROW_STANDALONE]. You can also override the locale, but Java only seems to have locale support for a few languages. |
${myzoneoffset.format()}` or |
java.time class | methods | comment | example |
---|---|---|---|
LocalDate |
|
Can compare two LocalDate objects for equality. |
|
LocalDateTime |
|
Can compare two LocalDateTime objects for equality. |
|
LocalTime |
|
Can compare two LocalTime objects for equality. |
|