Built-ins for dates
Page Contents
string (when used with a date value)
This built-in converts a date to a string, with the specified formatting. (when the default format dictated by the date_format, time_format and datetime_format settings of FreeMarker are good for you, then you do not need this built-in.)
The format can be one of the predefined formats, or you can specify the formatting pattern explicitly.
The predefined formats are short, medium, long, and full which define how verbose the resulting text will be. For example, if the locale of the output is U.S. English, and the time zone is the U.S. Pacific Time zone, then this:
|
|||
will prints something like this:
|
|||
The exact meaning of short, medium, long, and full depends on the current locale (language). Furthermore, it is specified not by FreeMarker, but the Java platform implementation you run FreeMarker on.
For dates that contains both date and time part, you can specify the length of the date and time part independently:
|
|||
will output:
|
|||
Note that ?string.short is the same as ?string.short_short, ?string.medium is the same as ?string.medium_medium, etc.
Warning!
Unfortunately, because of the limitations of the Java platform, it can happen that you have date variables in the data-model, where FreeMarker can't decide if the variable stores only date part (year, month, day), only time part (hour, minute, second, millisecond) or both. In this case, FreeMarker don't know how to display the date when you write something like ${lastUpdated?string.short} or simply ${lastUpdated}, and thus it will stop with error. To prevent this, you can help FreeMarker with the ?date, ?time and ?datetime built-ins. For example: ${lastUpdated?datetime?string.short}. Ask the programmer if certain variables of the data-model has this problem, or always use ?date, ?time and ?datetime built-ins.
Instead of using the predefined formats, you can specify the formatting pattern explicitly with ?string(pattern_string). The pattern uses Java date format syntax. Example:
|
|||
will output:
|
|||
Note
Unlike with the predefined formats, you never need to use ?date, ?time and ?datetime with explicitly given patterns, since with the pattern you tell FreeMarker what parts of the date to show. However, FreeMarker will trust you blindly, so you can show "noise" if you display parts that are actually not stored in the variable. For example, ${openingTime?string("yyyy-MM-dd hh:mm:ss a")}, where openingTime stores only time, will display 1970-01-01 09:24:44 PM.
The pattern string also can be "short", "medium", ..., "short_medium", ...etc. These are the same as if you would use the predefined formats with the dot syntax: someDate?string("short") and someDate?string.short are equivalent.
See also: the interpolation of dates
date, time, datetime (when used with a date value)
These built-ins can be used to specify which parts of the date variable are in use:
-
date: Only the year, month and day parts are used.
-
time: Only the hour, minute, second and millisecond parts are used.
-
datetime: Both the date and the time parts are used.
In optimal case, you do not need to use these built-ins. Unfortunately, because of the technical limitations of the Java platform, FreeMarker sometimes can't find out which parts of the date are in use (i.e. only the year+month+day, or only hour+minute+second+millisecond, or both); ask the programmers which variables has this problem. If FreeMarker has to execute an operation where this information is needed -- such as displaying the date as text -- but it does not know which parts are in use, it will stop with error. This is when you have to use these built-ins. For example, assume openingTime is a such problematic variable:
|
|||
There is another usage of these built-ins: to truncate dates. For example:
|
|||
will output something like:
|
|||
If the left side of the ? is string, then these built-ins convert strings to date variable.
iso_...
These built-ins convert a date, time or date-time value to string that follows ISO 8601 "extended" format. This built-in has several variations: iso_utc, iso_local, iso_utc_nz, iso_local_nz, iso_utc_m, iso_utc_m_nz, etc. The name is constructed from the following words in this order, each separated with a _ from the next:
-
iso (required)
-
Either utc or local (required (except when it's given with a parameter, but see that later)): Specifies whether you want to print the date according to UTC or according the current time zone. The current time zone is decided by the time_zone FreeMarker setting and is normally configured by the programmers outside the templates (but it can also be set in a template, like <#setting time_zone="America/New_York"> for example).
-
Either h or m or ms (optional): The accuracy of the time part. When omitted, it defaults to seconds accuracy (like 12:30:18). h means hours accuracy (like 12), m means minutes accuracy (12:30), and ms means milliseconds accuracy (12:30:18.25, where we have 250 ms). Note that when using ms, the milliseconds are displayed as fraction seconds (following the standard) and will not have trailing 0-s. Thus, if the the millisecond part happens to be 0, the whole fraction second part will be omitted. Also note that the fraction seconds are always separated with a dot , not with comma (to follow the Web conventions and the XML Schema date/time format).
-
nz (optional): When present, the time zone offset (like +02:00 or or -04:30 or Z) will not be displayed. Otherwise it will be displayed, except for date-only values (as dates with zone offset doesn't appear in ISO 8601:2004). Since FreeMarker 2.3.19, the offset always contains the minutes for XML Schema date/time format compliance.
Example:
|
|||
A possible output (depends on current time and time zone):
|
|||
There is yet another group of iso_... built-in variants, where you omit the local or utc word from the name and instead specify the time zone as a parameter to the built-in. Example:
|
|||
A possible output (depends on current time and time zone):
|
|||
If the time zone parameter can't be interpreted, the template processing will be terminated with error.
The parameter can be a java.util.TimeZone object too (which is possibly the return value of a Java method, or it's in the data-model), not just a string.
相关推荐
赠送jar包:freemarker-2.3.31.jar; 赠送原API文档:freemarker-2.3.31-javadoc.jar; 赠送源代码:freemarker-2.3.31-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.31.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.30.jar; 赠送原API文档:freemarker-2.3.30-javadoc.jar; 赠送源代码:freemarker-2.3.30-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.30.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.30.jar; 赠送原API文档:freemarker-2.3.30-javadoc.jar; 赠送源代码:freemarker-2.3.30-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.30.pom; 包含翻译后的API文档:...
FreeMarkerIDE-1.0.0.GA.zip是一款专为FreeMarker设计的Eclipse插件,旨在提升开发人员构建和管理FreeMarker应用的效率。FreeMarker是一个强大的模板引擎,广泛应用于Java环境中,用于生成动态HTML或其他格式的文本...
camel-freemarker-1.6.4.jar, camel-freemarker-2.8.1.jar, com.springsource.freemarker-2.3.15.jar, com.springsource.freemarker-sources-2.3.15.jar, freemarker-1.4.1.jar, freemarker-2-3-18.jar, freemarker-...
JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-...
赠送jar包:freemarker-2.3.23.jar; 赠送原API文档:freemarker-2.3.23-javadoc.jar; 赠送源代码:freemarker-2.3.23-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.23.pom; 包含翻译后的API文档:...
`freemarker-ide-0.9.14` 是一个针对Freemarker模板语言的集成开发环境插件,旨在提升在MyEclipse中的开发效率和体验。 这个插件版本为0.9.14,可能包含了对Freemarker语法的高亮显示、代码自动完成、错误检查、...
在这个"freemarker-2.3.22"的压缩包中,包含了Freemarker 2.3.22版本的相关资源,有助于我们深入了解和使用这个库。 首先,`README.txt`通常包含项目的基本信息、安装指南和快速入门等内容。在这个版本中,它可能...
赠送jar包:freemarker-2.3.20.jar; 赠送原API文档:freemarker-2.3.20-javadoc.jar; 赠送源代码:freemarker-2.3.20-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.20.pom; 包含翻译后的API文档:...
本压缩包"freemarker-ide-0.9.14.rar"提供的是一个针对IDE的Freemarker插件,特别适用于MyEclipse 6.5版本。 该插件名为"Hudson Freemarker IDE",版本为0.9.14,它增强了MyEclipse对Freemarker模板语言的支持。在...
这个"apache-freemarker-2.3.26-incubating-bin.tar.gz"文件是Apache FreeMarker的一个特定版本,即2.3.26,处于孵化器阶段(incubating)。它包含了FreeMarker库的二进制文件和相关的文档,为开发者提供了完整的...
在本Demo中,我们将深入探讨如何将FreeMarker模板引擎整合进SSM框架,以及PageHelper分页插件的使用。 首先,让我们来了解一下SSM框架的核心组件: 1. **Spring**:这是一个全面的Java应用框架,提供依赖注入(DI...
赠送jar包:freemarker-2.3.20.jar; 赠送原API文档:freemarker-2.3.20-javadoc.jar; 赠送源代码:freemarker-2.3.20-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.20.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.23.jar; 赠送原API文档:freemarker-2.3.23-javadoc.jar; 赠送源代码:freemarker-2.3.23-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.23.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.31.jar; 赠送原API文档:freemarker-2.3.31-javadoc.jar; 赠送源代码:freemarker-2.3.31-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.31.pom; 包含翻译后的API文档:...
这个“freemarker-2.3.23-中文手册.zip”包含了Freemarker 2.3.23版本的详尽中文文档,对于学习和掌握Freemarker的使用非常有帮助。 在Freemarker 2.3.23版中,我们首先会接触到它的核心概念,包括模板(Template)...
这个"freemarker-2.3.28.jar"是Freemarker库的一个具体版本,版本号为2.3.28,它是Java的一个可执行的JAR(Java Archive)文件,用于在Eclipse集成开发环境中作为插件使用。 在Freemarker的2.3.28版本中,我们可以...
【标题】"FreeMarkerDemo-java.rar" 是一个与Spring Boot相关的示例项目,它演示了如何将Word文档(docx格式)转换为FreeMarker(ftl)模板。这个压缩包包含了一个Java应用,该应用利用Spring Boot的强大功能,以及...