有人知道 FreeMarker 有支持新 java.time api 的计划吗?或者是否有任何代码可以支持 ZonedDateTime、LocalDateTime 和 Instant 等类?
自己实现这些东西并不难,但实际上是一个相当大的任务。
假设您想要格式化新的日期/时间对象
-
创建自定义方法:
public static class FormatDateTimeMethodModel
implements TemplateMethodModelEx {
public Object exec(List args) throws TemplateModelException {
if (args.size() != 2) {
throw new TemplateModelException("Wrong arguments");
}
TemporalAccessor time = (TemporalAccessor) ((StringModel) args.get(0)).getWrappedObject();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(((SimpleScalar) args.get(1)).getAsString());
return formatter.format(time);
}
}
-
将此方法放入模板模型中:
templateModel.put("formatDateTime", new FormatDateTimeMethodModel());
-
并在模板内调用此方法:
${formatDateTime(MY_DATE, 'HH:mm')}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)