private DateTimeFormatter formatter(JsonDateFormat ann, DateTimeFormatter defaultFormatter) { Locale locale = ann.lang().isEmpty() ? Locale.getDefault() : new Locale(ann.lang()); if (ann.value() == null || ann.value().isEmpty()) return defaultFormatter; else return DateTimeFormat.forPattern(ann.value()).withLocale(locale); } }
@Override public Converter create(BeanProperty property, Genson genson) { JsonDateFormat ann = property.getAnnotation(JsonDateFormat.class); if (ann != null) { Locale locale = ann.lang().isEmpty() ? Locale.getDefault() : new Locale( ann.lang()); DateFormat dateFormat = ann.value() != null && !ann.value().isEmpty() ? new SimpleDateFormat(ann.value(), locale) : SimpleDateFormat.getInstance(); if (Date.class.isAssignableFrom(property.getRawClass())) return new DateConverter(dateFormat, ann.asTimeInMillis()); if (Calendar.class.isAssignableFrom(property.getRawClass())) return new CalendarConverter( new DateConverter(dateFormat, ann.asTimeInMillis())); } return null; } }
private DateTimeConverterOptions createOptions(BeanProperty property, Class<?> clazz) { JsonDateFormat jsonDateFormat = property.getAnnotation(JsonDateFormat.class); DateTimeFormatter formatter = getFormatter(jsonDateFormat, formatters.get(clazz)); boolean asTimestamp = jsonDateFormat == null ? timestampByDefault : jsonDateFormat.asTimeInMillis(); ZoneId zoneId = getZoneId(property); TimestampFormat timestampFormat = getTimestampFormat(property, getDefaultTimestampFormat(clazz)); return new DateTimeConverterOptions(clazz, formatter, asTimestamp, timestampFormat, zoneId); }
@Override public Converter create(BeanProperty property, Genson genson) { JsonDateFormat ann = property.getAnnotation(JsonDateFormat.class); if (ann != null) { if (MutableDateTime.class.isAssignableFrom(property.getRawClass())) { return makeMutableDateTimeConverter(ann.asTimeInMillis(), formatter(ann, dateTimeFormatter)); } else if (DateTime.class.isAssignableFrom(property.getRawClass())) { return makeDateTimeConverter(ann.asTimeInMillis(), formatter(ann, dateTimeFormatter)); } // local formats else if (LocalDate.class.isAssignableFrom(property.getRawClass())) { return makeLocalDateConverter(formatter(ann, localDateFormatter)); } else if (LocalDateTime.class.isAssignableFrom(property.getRawClass())) { return makeLocalDateTimeConverter(formatter(ann, localDateTimeFormatter)); } else if (LocalTime.class.isAssignableFrom(property.getRawClass())) { return makeLocalTimeConverter(formatter(ann, localTimeFormatter)); } } return null; }
private DateTimeFormatter formatter(JsonDateFormat ann, DateTimeFormatter defaultFormatter) { Locale locale = ann.lang().isEmpty() ? Locale.getDefault() : new Locale(ann.lang()); if (ann.value() == null || ann.value().isEmpty()) return defaultFormatter; else return DateTimeFormat.forPattern(ann.value()).withLocale(locale); } }
@Override public Converter create(BeanProperty property, Genson genson) { JsonDateFormat ann = property.getAnnotation(JsonDateFormat.class); if (ann != null) { Locale locale = ann.lang().isEmpty() ? Locale.getDefault() : new Locale( ann.lang()); DateFormat dateFormat = ann.value() != null && !ann.value().isEmpty() ? new SimpleDateFormat(ann.value(), locale) : SimpleDateFormat.getInstance(); if (Date.class.isAssignableFrom(property.getRawClass())) return new DateConverter(dateFormat, ann.asTimeInMillis()); if (Calendar.class.isAssignableFrom(property.getRawClass())) return new CalendarConverter( new DateConverter(dateFormat, ann.asTimeInMillis())); } return null; } }
@Override public Converter create(BeanProperty property, Genson genson) { JsonDateFormat ann = property.getAnnotation(JsonDateFormat.class); if (ann != null) { if (MutableDateTime.class.isAssignableFrom(property.getRawClass())) { return makeMutableDateTimeConverter(ann.asTimeInMillis(), formatter(ann, dateTimeFormatter)); } else if (DateTime.class.isAssignableFrom(property.getRawClass())) { return makeDateTimeConverter(ann.asTimeInMillis(), formatter(ann, dateTimeFormatter)); } // local formats else if (LocalDate.class.isAssignableFrom(property.getRawClass())) { return makeLocalDateConverter(formatter(ann, localDateFormatter)); } else if (LocalDateTime.class.isAssignableFrom(property.getRawClass())) { return makeLocalDateTimeConverter(formatter(ann, localDateTimeFormatter)); } else if (LocalTime.class.isAssignableFrom(property.getRawClass())) { return makeLocalTimeConverter(formatter(ann, localTimeFormatter)); } } return null; }
private DateTimeFormatter getFormatter(JsonDateFormat ann, DateTimeFormatter defaultFormatter) { if(ann == null || ann.value().isEmpty()){ return defaultFormatter; } else { Locale locale = ann.lang().isEmpty() ? Locale.getDefault() : new Locale(ann.lang()); return DateTimeFormatter.ofPattern(ann.value()).withLocale(locale); } }
@Override public Converter create(BeanProperty property, Genson genson) { if (!Temporal.class.isAssignableFrom(property.getRawClass())) return null; // only deal with java.time String format = null, lang = null; Format cfg1 = property.getAnnotation(Format.class); // support simter Format annotation if (null != cfg1) { format = cfg1.value(); lang = cfg1.lang(); } else { JsonDateFormat cfg2 = property.getAnnotation(JsonDateFormat.class); // support genson JsonDateFormat annotation if (null != cfg2) { format = cfg2.value(); lang = cfg2.lang(); } } return getConverter(property.getRawClass(), format, lang); }