有人知道在 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')}
关于java-8 - Freemarker 中的 Java.time (Java 8) 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32063276/