This project provides JSP tags for the new java.time package present in Java 8.
The java.time packages are specified in JSR-310 and are based on the Joda-Time library.
This project is forked from and based on the original Joda-Time JSP Tags.
I started this project because I needed a replacement of Joda-Time JSP Tags after migration of a project to Java 8.
The library is pretty stable right now. Should you have any problem please file an issue. There is no planned development for this library, just bugfix maintenance.
TL;DR Use only v2.x of the library if you are migrating your project to the new Jakarta EE 9 or higher with the new package names.
Version 2.0.0 switches to the new Jakarta EE package naming. No other changes are introduced but a major version is used as it will break compilation of existing code. Thanks to Jon Schewe for the contribution that made this possible.
This library works very similarly to the date-related tags in the jstl fmt library and almost exactly as the tags in the original Joda-Time JSP Tags.
- Java 17
- Servlet 5.0
- JSP 3.0
- JSTL 2.0
Add the dependency to your project:
compile 'net.sargue:java-time-jsptags:2.0.0'
<dependency>
<groupId>net.sargue</groupId>
<artifactId>java-time-jsptags</artifactId>
<version>2.0.0</version>
</dependency>
Declare the library as follows in your jsp pages:
<%@ taglib uri="http://sargue.net/jsptags/time" prefix="javatime" %>
You can browse online the javadocs thanks to the great javadoc.io service.
Most tags have the attributes style
and pattern
which control the formatter beneath the tag.
The style
expected value is two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None.
They directly map to the enum FormatStyle
The pattern
attribute is for complete control over your formatting.
The syntax is explained in the DateTimeFormatter
A time zone may be necessary to perform some formatting. It depends on the
desired format and the value object. An Instant
has no time zone so if you
want a long style time format (which outputs the time zone) you will need
to adjust using a time zone. A ZonedDateTime
has all the information needed
but you may want to change the display time zone.
The time zone may be specified using an attribute, an enclosing
<javatime:zoneId/>
tag, preceding <javatime:setZoneId/>
tag, or via the
net.sargue.time.zoneId
scoped variable.
The time zone will default to the system default time-zone if none is specified and the value object is one of these classes:
Instant
LocalDateTime
LocalTime
OffsetDateTime
OffsetLocalTime
Example:
<javatime:format value="${dt}" style="MS" />
Formats any java.util.Temporal
like Instant
, LocalDateTime
, LocalDate
, LocalTime
, etc.
The var
and scope
attributes can be used to set the value of a variable instead of printing the result.
Attributes:
Attribute | Description |
---|---|
value (required) | Must be a Temporal. |
var | The scoped variable to set. |
scope | The scope of the variable to set. |
locale | The locale to use for formatting. |
style | The style to use for formatting (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None) |
pattern | The pattern to use for formatting (see java.time format documentation for recognized pattern strings) |
zoneId | The time zone to use for formatting. See comment above for fallback and defaults. |
Example:
<javatime:parseInstant value="${dt}" style="MS" />
Parses a string into an java.time.Instant
object.
The var
and scope
attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing <javatime:zoneId/>
tag,
preceding <javatime:setZoneId/>
tag, or via the net.sargue.time.zoneId
scoped variable.
Attributes:
Attribute | Description |
---|---|
value | Required. Must be a string which can be parsed into a java.time.Instant according to the parsing options specified. |
var | The scoped variable to set. |
scope | The scope of the variable to set. |
locale | The locale to use for parsing. |
style | The style to use for parsing (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None) |
pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
Example:
<javatime:parseLocalDateTime value="${dt}" style="MS" />
Parses a string into an java.time.LocalDateTime
object.
The var
and scope
attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing <javatime:zoneId/>
tag,
preceding <javatime:setZoneId/>
tag, or via the net.sargue.time.zoneId
scoped variable.
Attributes:
Attribute | Description |
---|---|
value | Required. Must be a string which can be parsed into a java.time.LocalDateTime according to the parsing options specified. |
var | The scoped variable to set. |
scope | The scope of the variable to set. |
locale | The locale to use for parsing. |
style | The style to use for parsing (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None) |
pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
Example:
<javatime:parseLocalDate value="28/10/2015" pattern="dd/MM/yyyy" />
Parses a string into an java.time.LocalDate
object.
The var
and scope
attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing <javatime:zoneId/>
tag,
preceding <javatime:setZoneId/>
tag, or via the net.sargue.time.zoneId
scoped variable.
Attributes:
Attribute | Description |
---|---|
value | Required. Must be a string which can be parsed into a java.time.LocalDate according to the parsing options specified. |
var | The scoped variable to set. |
scope | The scope of the variable to set. |
locale | The locale to use for parsing. |
style | The style to use for parsing (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None) |
pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
Example:
<javatime:parseLocalTime value="10:43" pattern="HH:mm" />
Parses a string into an java.time.LocalTime
object.
The var
and scope
attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing <javatime:zoneId/>
tag,
preceding <javatime:setZoneId/>
tag, or via the net.sargue.time.zoneId
scoped variable.
Attributes:
Attribute | Description |
---|---|
value | Required. Must be a string which can be parsed into a java.time.LocalTime according to the parsing options specified. |
var | The scoped variable to set. |
scope | The scope of the variable to set. |
locale | The locale to use for parsing. |
style | The style to use for parsing (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None) |
pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
Example:
<javatime:zoneId value="Asia/Bangkok">
<javatime:format value="${dt}" style="MS" />
</javatime:zoneId>
Provides a default time zone to all <javatime:format/>
tags which are nested within it.
The <javatime:format/>
tag may override this value with an explicit zoneId
attribute.
Attribute | Description |
---|---|
value (required) | The default time zone for nested tags to use. |
Example:
<javatime:setZoneId value="Asia/Bangkok" />
<javatime:format value="${dt}" style="MS" />
Sets the time zone object in the given scoped variable.
If var
is not specified, it will be stored in a scoped variable called net.sargue.time.zoneId
.
The <javatime:format/>
tag will default to using a time zone stored under this name if it does not have
a zoneId
attribute and is not nested within a <javatime:zoneId/>
tag.
Attribute | Description |
---|---|
value (required) | The time zone to set. |
var | The scoped variable to set. |
scope | The scope of the variable to set. |
Build is based on gradle. See build.gradle
included in the repository.
Changed dependency types to "compileOnly" so this library is not leaking specific JSP/JSTL libraries.
Updated for jakarta package names for J2EE classes.
Requires Java 17 now due to dependency on spring-test 6.0.
Made helper method public by request.
Fixed issue #5 about error messages.
I have changed the gradle build to use the gradle wrapper and gradle version 2.12 which finally includes a compile-only (like provided) configuration. I have updated the build script acordingly. It shouldn't break any build but I detected that including this library before this change leaked some undesired jar files (like the JSTL API).
Fixed issue #2, better support of time zones on formatting.
Fixed issue #1, added more parse tags.
Some tests added. Minor refactorings and no functionality changed. Some documentation. Moved to gradle build. Preparing to publish to Maven Central.
First released version just with some refactoring, no tests, no documentation.
If you found any bug please report it to the GitHub issues page.
PR are welcome but please try to be clear and provide some tests.