如何为VelocityTools DateTool设置TimeZone?

时间:2012-08-10 18:36:50

标签: velocity

VelocityTools Tools Usage Summarythe DateTool Javadoc似乎都表示设置TimeZone的配置机制,但我根本无法发现它是什么。

DateTool的时区设置器是protected,我不想重复将TimeZone传递给重载的format()方法。

(VelocityTools 2.0)

1 个答案:

答案 0 :(得分:7)

要将配置值传递给标准工具,您必须在WEB-INF / tools.xml文件中明确列出它们:

<?xml version="1.0"?>
<tools>
  <toolbox scope="application">
    <tool key="date"
          class="org.apache.velocity.tools.generic.DateTool"
          timezone="GMT+7"/>
    ...
  </toolbox>
  ...
</tools>

请注意,您可以使用org.apache.velocity.tools.generic.ComparisonDateTool用于相同目的,这会添加一些不错的日期时间比较功能。

如果您不依赖于VelocityView工具加载机制,那么这意味着您将自己的工具放在Velocity上下文中。如果是,则配置值将手动提供给工具configure(Map)。例如:

Map<String,String> config = new HashMap<>();
config.put(DateTool.TIMEZONE_KEY,"GMT+7");

DateTool date = new DateTool();
date.configure(config);

VelocityContext context = new VelocityContext();
context.put("date", date);