Java.time (Java 8) support in Freemarker

后端 未结 3 639
眼角桃花
眼角桃花 2021-01-18 02:28

Does anybody know of any plans to support the new java.time api in FreeMarker? Or has anybody code laying around for supporting classes like ZonedDateTime, LocalDateTime and

3条回答
  •  天命终不由人
    2021-01-18 03:00

    private static class CustomObjectWrapper extends DefaultObjectWrapper {
        @Override
        public TemplateModel wrap(Object obj) throws TemplateModelException {
            if (obj instanceof LocalDateTime) {
                Timestamp timestamp = Timestamp.valueOf((LocalDateTime) obj);
                return new SimpleDate(timestamp);
            }
            if (obj instanceof LocalDate) {
                Date date = Date.valueOf((LocalDate) obj);
                return new SimpleDate(date);
            }
            if (obj instanceof LocalTime) {
                Time time = Time.valueOf((LocalTime) obj);
                return new SimpleDate(time);
            }
            return super.wrap(obj);
        }
    }
    
    
    @Autowired
    private freemarker.template.Configuration configuration;
    
    configuration.setObjectWrapper(new CustomObjectWrapper());
    

提交回复
热议问题