@Test public void testConverter() { String pattern = "yyyy.MM.dd HH:mm:ss.SSS"; SimpleDateFormat format = new SimpleDateFormat(pattern); ObjectToDateConverter converter = new ObjectToDateConverter(pattern); Date now = new Date(); Calendar cal = Calendar.getInstance(); Calendar midnight = Calendar.getInstance(); midnight.set(Calendar.HOUR_OF_DAY, 0); midnight.set(Calendar.MINUTE, 0); midnight.set(Calendar.SECOND, 0); midnight.set(Calendar.MILLISECOND, 0); LocalDateTime dateTime = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault()); LocalDate date = LocalDateTime.ofInstant(midnight.toInstant(), ZoneId.systemDefault()).toLocalDate(); assertNull(converter.convert(Float.MIN_NORMAL)); assertEquals(now, converter.convert(BigDecimal.valueOf(now.getTime()))); assertEquals(now, converter.convert(now)); assertEquals(now, converter.convert(now.getTime())); assertEquals(cal.getTime(), converter.convert(cal)); assertEquals(now, converter.convert(format.format(now))); assertEquals(now, converter.convert(dateTime)); assertEquals(midnight.getTime(), converter.convert(date)); }
@Override public Date convert(Object value) { Date converted = null; if (value instanceof Date) { converted = (Date) value; } else if (value instanceof Calendar) { converted = ((Calendar) value).getTime(); } else if (value instanceof LocalDateTime) { converted = Date.from(((LocalDateTime) value) .atZone(ZoneId.systemDefault()).toInstant()); } else if (value instanceof LocalDate) { converted = Date.from(((LocalDate) value) .atStartOfDay(ZoneId.systemDefault()).toInstant()); } else if (value instanceof Long) { converted = new Date((Long) value); } else if (value instanceof BigDecimal) { converted = new Date(((BigDecimal) value).longValue()); } else if (value instanceof String && pattern != null) { try { converted = new SimpleDateFormat(pattern).parse((String) value); } catch (ParseException e) { throw new MustacheException( MustacheProblem.RENDER_GENERIC_ERROR, "Unable to parse the string value %s using pattern %s", value, pattern); } } return converted; }
ObjectToDateConverter implements Converter<Object, Date> { @Override public Date convert(Object value) { Date converted = null; if (value instanceof Date) { converted = (Date) value; } else if (value instanceof Calendar) { converted = ((Calendar) value).getTime(); } else if (value instanceof LocalDateTime) { converted = Date.from(((LocalDateTime) value) .atZone(ZoneId.systemDefault()).toInstant()); } else if (value instanceof LocalDate) { converted = Date.from(((LocalDate) value) .atStartOfDay(ZoneId.systemDefault()).toInstant()); } else if (value instanceof Long) { converted = new Date((Long) value); } else if (value instanceof BigDecimal) { converted = new Date(((BigDecimal) value).longValue()); } else if (value instanceof String && pattern != null) { try { converted = new SimpleDateFormat(pattern).parse((String) value); } catch (ParseException e) { throw new MustacheException( MustacheProblem.RENDER_GENERIC_ERROR, "Unable to parse the string value %s using pattern %s", value, pattern); } } return converted; } }
ObjectToDateConverter implements Converter<Object, Date> { @Override public Date convert(Object value) { Date converted = null; if (value instanceof Date) { converted = (Date) value; } else if (value instanceof Calendar) { converted = ((Calendar) value).getTime(); } else if (value instanceof LocalDateTime) { converted = Date.from(((LocalDateTime) value) .atZone(ZoneId.systemDefault()).toInstant()); } else if (value instanceof LocalDate) { converted = Date.from(((LocalDate) value) .atStartOfDay(ZoneId.systemDefault()).toInstant()); } else if (value instanceof Long) { converted = new Date((Long) value); } else if (value instanceof BigDecimal) { converted = new Date(((BigDecimal) value).longValue()); } else if (value instanceof String && pattern != null) { try { converted = new SimpleDateFormat(pattern).parse((String) value); } catch (ParseException e) { throw new MustacheException( MustacheProblem.RENDER_GENERIC_ERROR, "Unable to parse the string value %s using pattern %s", value, pattern); } } return converted; } ObjectToDateConverter();  ObjectToDateConverter(String pattern); }
ObjectToDateConverter implements Converter<Object, Date> { @Override public Date convert(Object value) { Date converted = null; if (value instanceof Date) { converted = (Date) value; } else if (value instanceof Calendar) { converted = ((Calendar) value).getTime(); } else if (value instanceof LocalDateTime) { converted = Date.from(((LocalDateTime) value) .atZone(ZoneId.systemDefault()).toInstant()); } else if (value instanceof LocalDate) { converted = Date.from(((LocalDate) value) .atStartOfDay(ZoneId.systemDefault()).toInstant()); } else if (value instanceof Long) { converted = new Date((Long) value); } else if (value instanceof BigDecimal) { converted = new Date(((BigDecimal) value).longValue()); } else if (value instanceof String && pattern != null) { try { converted = new SimpleDateFormat(pattern).parse((String) value); } catch (ParseException e) { throw new MustacheException( MustacheProblem.RENDER_GENERIC_ERROR, "Unable to parse the string value %s using pattern %s", value, pattern); } } return converted; } ObjectToDateConverter();  ObjectToDateConverter(String pattern); @Override Date convert(Object value); }
ObjectToDateConverter implements Converter<Object, Date> { @Override public Date convert(Object value) { Date converted = null; if (value instanceof Date) { converted = (Date) value; } else if (value instanceof Calendar) { converted = ((Calendar) value).getTime(); } else if (value instanceof LocalDateTime) { converted = Date.from(((LocalDateTime) value) .atZone(ZoneId.systemDefault()).toInstant()); } else if (value instanceof LocalDate) { converted = Date.from(((LocalDate) value) .atStartOfDay(ZoneId.systemDefault()).toInstant()); } else if (value instanceof Long) { converted = new Date((Long) value); } else if (value instanceof BigDecimal) { converted = new Date(((BigDecimal) value).longValue()); } else if (value instanceof String && pattern != null) { try { converted = new SimpleDateFormat(pattern).parse((String) value); } catch (ParseException e) { throw new MustacheException( MustacheProblem.RENDER_GENERIC_ERROR, "Unable to parse the string value %s using pattern %s", value, pattern); } } return converted; } ObjectToDateConverter();  ObjectToDateConverter(String pattern); @Override Date convert(Object value);  }