All Projects → lazee → Freemarker Java 8

lazee / Freemarker Java 8

Licence: apache-2.0
Library adding java.time support to FreeMarker

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Freemarker Java 8

Hahu
参照知乎网做的一个简易版问答网站,后台采用Java语言,及Spring,SpringMVC,MyBatis等框架,模版引擎使用FreeMarker。数据库用到MySQL,Redis。
Stars: ✭ 408 (+1002.7%)
Mutual labels:  freemarker, maven
Publiccms
现代化java cms,由天津黑核科技有限公司开发,轻松支撑千万数据、千万PV;支持静态化,服务器端包含; 目前已经拥有全球0.0002%的用户,语言支持中、繁、日、英;是一个已走向海外的成熟CMS产品
Stars: ✭ 1,750 (+4629.73%)
Mutual labels:  freemarker, maven
Open Solution Value Prediction
Open solution to the Santander Value Prediction Challenge 🐠
Stars: ✭ 34 (-8.11%)
Mutual labels:  open-source
Comcast.github.io
The main Open Source portal for Comcast
Stars: ✭ 36 (-2.7%)
Mutual labels:  open-source
Blogmanageplatform
一个springboot项目的脚手架,追求简洁高速可扩展。
Stars: ✭ 34 (-8.11%)
Mutual labels:  freemarker
Monogame
One framework for creating powerful cross-platform games.
Stars: ✭ 8,014 (+21559.46%)
Mutual labels:  open-source
Android Studio Templates
A set of templates for your Android Studio
Stars: ✭ 35 (-5.41%)
Mutual labels:  freemarker
Hacktoberfest2k19
Hacktoberfest is here! Raise the PR and earn goodies.
Stars: ✭ 34 (-8.11%)
Mutual labels:  open-source
Pupil
Open source eye tracking
Stars: ✭ 982 (+2554.05%)
Mutual labels:  open-source
Plantuml Styler
Online tool to make your PlantUML diagrams look great.
Stars: ✭ 35 (-5.41%)
Mutual labels:  open-source
Awesome Ngo
A curated list of FOSS (Free and Open-source Software), freemium tools and guides for NGOs
Stars: ✭ 36 (-2.7%)
Mutual labels:  open-source
Spicyclient
This is the code for SpicyClient
Stars: ✭ 35 (-5.41%)
Mutual labels:  open-source
Mahapps.metro
A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
Stars: ✭ 8,023 (+21583.78%)
Mutual labels:  open-source
Startbootstrap Round About
A Bootstrap HTML template for creating about pages - created by Start Bootstrap
Stars: ✭ 35 (-5.41%)
Mutual labels:  open-source
Calla
Virtual Meetups through Jitsi
Stars: ✭ 971 (+2524.32%)
Mutual labels:  open-source
Aliyungradleconfig
自用的安卓开源项目工程模板。阿里云远程仓库加速,发布到maven仓库、上传到jcenter,代码混淆、资源混淆,持续集成(CI),多渠道自动打包。在天朝使用jcenter、mavenCentral及google三个远程仓库,Gradle Sync太慢?一招教你配置阿里云镜像源。init.d/init.gradle
Stars: ✭ 37 (+0%)
Mutual labels:  maven
Passwordcockpit
Passwordcockpit is a simple, free, open source, self hosted, web based password manager for teams. It is made in PHP, Javascript, MySQL and it run on a docker service. It allows users with any kind of device to safely store, share and retrieve passwords, certificates, files and much more.
Stars: ✭ 34 (-8.11%)
Mutual labels:  open-source
Healthcheck
Health Check ✔ is a Machine Learning Web Application made using Flask that can predict mainly three diseases i.e. Diabetes, Heart Disease, and Cancer.
Stars: ✭ 35 (-5.41%)
Mutual labels:  open-source
Source two
Open Source FPV Racing Frame
Stars: ✭ 35 (-5.41%)
Mutual labels:  open-source
Nsfw Filter
🚀 A Google Chrome / Firefox extension that blocks NSFW images from the web pages that you load using TensorFlow JS.
Stars: ✭ 984 (+2559.46%)
Mutual labels:  open-source

java.time support for FreeMarker

FJ8 (freemarker-java-8) is a Java library that adds java.time api support to FreeMarker. It is easy to add to your codebase, and very easy to use.

Basically this library allows you to format and print values from java.time classes within FreeMarker templates. As a bonus you also get some comparison functions.

It is not a perfect solution as FreeMarker doesn’t support custom built-ins. Hopefully future versions of FreeMarker will add native support, but it doesn't look promising (http://freemarker.org/contribute.html).

Basically this library allows you to format java.time types within your templates, using the new java.time.format.DateTimeFormatter.

Table of content

Installation

You need Java 8 or higher. FJ8 is tested on Freemarker 2.3.23, and should at least work fine for all 2.3.x versions.

Maven

<dependency>
    <groupId>no.api.freemarker</groupId>
    <artifactId>freemarker-java8</artifactId>
    <version>2.0.0</version>
</dependency>

Gradle

implementation 'no.api.freemarker:freemarker-java8:2.0.0'

Setup

FJ8 extends the DefaultObjectWrapper to add support for the java.time classes. All you need to do is to replace the default object wrapper with the FJ8 implementation in your FreeMarker Configuration object.

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));

Spring setup

This is how you can add FJ8 to your FreeMarker configuration in Spring / Spring Boot.

package com.example.demo;

import no.api.freemarker.java8.Java8ObjectWrapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

@Configuration
public class FreemarkerConfig implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        if (bean instanceof FreeMarkerConfigurer) {
            FreeMarkerConfigurer configurer = (FreeMarkerConfigurer) bean;
            configurer.getConfiguration().setObjectWrapper(new Java8ObjectWrapper(freemarker.template.Configuration.getVersion()));
        }
        return bean;
    }
}

Thanks to Desson Ariawan for the example

Upgrade from 1.3 to 2.0

The 2.0 release addresses two major issues reported by users (#18/#16). It also introduces a new feature for manipulating time (#28).

The upgrade itself is nothing else than changing the version in your build configuration (pom.xml or something else). However if you need to stick to the old behaviour on how time zones are treated when formatting ZonedDateTime objects, then you need to add a second argument to Java8ObjectWrapper upon initialization:

configuration.setObjectWrapper(
	new Java8ObjectWrapper(VERSION_2_3_23, new EnvironmentTimeStrategy()
);

Usage

Formatting java.time classes

All format methods uses the java.time.format.DateTimeFormatter for formatting.

☑️ java.time.Clock

This is a simple implementation where format just prints the toString() value of the object.

Methods

  • format()

Example

${myclock.format()}

java.time.Duration

Gives access to the Duration values.

Methods

  • nano()
  • seconds()

Example

${myduration.seconds}
${myduration.nano}

☑️ java.time.Instant

This is a simple implementation where format just prints the toString() value of the object.

Methods

  • format()
  • format(pattern)

Example

${myinstant.format()}

☑️ java.time.LocalDate

Allows you to print a LocalDate on a default pattern, by providing a custom pattern or a builtin format style.

Methods

  • format()

Example

${mylocaldate.format()}
${mylocaldate.format('yyyy MM dd')}
${mylocaldate.format('FULL_DATE')}

☑️ java.time.LocalDateTime

Allows you to print a LocalDateTime on a default pattern, by providing a custom pattern or a builtin format style.

Methods

  • format()
  • format(pattern)

Example

${mylocaldatetime.format()}
${mylocaldatetime.format('yyyy-MM-dd HH : mm : ss')}
${mylocaldatetime.format('MEDIUM_DATETIME')}

☑️ java.time.LocalTime

Allows you to print a LocalTime on a default pattern, by providing a custom pattern or a builtin format style.

Methods

  • format()
  • format(pattern)

Example

${mylocaltime.format()}
${mylocaltime.format('HH : mm : ss')}
${mylocaltime.format('SHORT_TIME')}

☑️ java.time.MonthDay

Allows you to print a MonthDay on a default pattern or by providing a custom pattern.

Methods

  • format()
  • format(pattern)

Example

${mymonthday.format()}
${mymonthday.format('MM dd')}

☑️ java.time.OffsetDateTime

Allows you to print a OffsetDateTime on a default pattern, by providing a custom pattern or a builtin format style.

Methods

  • format()
  • format(pattern)

Example

${myoffsetdatetime.format()}
${myoffsetdatetime.format('yyyy MM dd HH mm ss')}
${myoffsetdatetime.format('FULL_DATETIME')}

☑️ java.time.OffsetTime

Allows you to print a OffsetTime on a default pattern, by providing a custom pattern or a builtin format style.

Methods

  • format()
  • format(pattern)

Example

${myoffsettime.format()}
${myoffsettime.format('HH mm ss')}
${myoffsettime.format('MEDIUM_TIME')}

☑️ java.time.Period

Provides access to the values of the a Period object within your template.

Methods

  • days()
  • months()
  • years()

Example

${myperiod.days}
${myperiod.months}
${myperiod.years}

☑️ java.time.Year

Allows you to print a Year on a default pattern or by providing a custom pattern.

Methods

  • format()
  • format(pattern)

Example

${myyear.format()}
${myyear.format('yyyy')}

☑️ java.time.YearMonth

Allows you to print a YearMonth on a default pattern or by providing a custom pattern.

Methods

  • format()
  • format(pattern)

Example

${myyear.format()}
${myyear.format('yyyy MM')}

☑️ java.time.ZonedDateTime

Allows you to print a YearMonth on a default pattern/timezone or by providing a custom pattern.

Methods

  • format()
  • format(pattern)
  • format(pattern, zone)

Example

${myzoneddatetime.format()}
${myzoneddatetime.format('yyyy-MM-dd Z')}
${myzoneddatetime.format('yyyy-MM-dd Z', 'Asia/Seoul')}

Notice

When a zone is not set, the formatter will use the zone found in the ZonedDateTime object itself. This behaviour can be changed if you want to. Scenarious where that might come in handy could be when you always wants to convert the timezone into your local timezone.

Java8ObjectMappernow takes a second argument where you can choose one of four strategies for the time zone used when formatting a ZonedDateTime:

  • EnviromentZonedDateTimeStrategy - Will convert the time zone into the one currently set within Freemarker.
  • KeepingZonedDateTimeStrategy - Will use the zone from the ZonedDateTime object itself (DEFAULT)
  • SystemZonedDateTimeStrategy - Will convert the time zone into ZoneId.systemDefault().
  • StaticSystemZoneDateTimeStrategy - Will use the time zone set when creating this strategy.

Example:

new Java8ObjectWrapper(VERSION_2_3_23, new EnvironmentZonedDateTimeStrategy());
// or
new Java8ObjectWrapper(VERSION_2_3_23, new StaticZonedDateTimeStrategy(ZoneId.of("Europe/Oslo")));

☑️ java.time.ZonedId

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.

Methods

  • format()
  • format(textStyle)
  • format(textstyle, locale)

Example

${myzoneid.format()}
${myzoneid.format('short')}
${myzoneid.format('short', 'no-NO')}

☑️ java.time.ZonedOffset

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.

Methods

  • format()
  • format(textStyle)

Example

${myzoneoffset.format()}
${myzoneoffset.format('short')}

Comparison

☑️ java.time.LocalDate

Can compare two LocalDate objects for equality.

Methods

  • isEqual(localDate)
  • isAfter(localDate)
  • isBefore(localDate)

Example

${localDate.isEqual(anotherlocalDate)}
${localDate.isAfter(anotherlocalDate)}
${localDate.isBefore(anotherlocalDate)}

☑️ java.time.LocalDateTime

Can compare two LocalDateTime objects for equality.

Methods

  • isEqual(localDateTime)
  • isAfter(localDateTime)
  • isBefore(localDateTime)

Example

${localDateTime.isEqual(anotherlocalDateTime)}
${localDateTime.isAfter(anotherlocalDateTime)}
${localDateTime.isBefore(anotherlocalDateTime)}

☑️ java.time.LocalTime

Can compare two LocalTime objects for equality.

Methods

  • isEqual(localDateTime)
  • isAfter(localDateTime)
  • isBefore(localDateTime)

Example

${localTime.isEqual(anotherlocalTime)}
${localTime.isAfter(anotherlocalTime)}
${localTime.isBefore(anotherlocalTime)}

Manipulating time

☑️ java.time.temporal.Temporal

Can create a new Temporal object with specified time difference from the original object, supporting

java.time.Instant, 
java.time.LocalDate, 
java.time.LocalDateTime, 
java.time.LocalTime, 
java.time.OffsetDateTime, 
java.time.OffsetTime, 
java.time.Year, 
java.time.YearMonth, 
java.time.ZonedDateTime

Methods

  • plusSeconds()
  • plusMinutes()
  • plusDays()
  • plusWeeks()
  • plusMonths()
  • plusYears()

Example

${localDateTime.plusMonths(1).plus.Hours(-2).plusMinutes(5).plusSeconds(30).format()}

Notice

Recently this repository was moved from the Amedia organisation to my private account on Github. The reason behind this is that I recently left Amedia after 13 years (!) and that they let me take this project with me. The package naming will however stay the same.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].