下面列出了org.joda.time.format.ISODateTimeFormat#dateTimeNoMillis ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Serializer() {
dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis();
localDateFormatter = ISODateTimeFormat.date();
objectMapper = new ObjectMapper()
.registerModule(new JodaModule()
.addSerializer(DateTime.class, new DateTimeSerializer()
.withFormat(new JacksonJodaDateFormat(dateTimeFormatter), 0)
)
.addSerializer(LocalDate.class, new LocalDateSerializer()
.withFormat(new JacksonJodaDateFormat(localDateFormatter), 0)
)
)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); // TODO: Deprecate in vNext
}
@Override
public DateTimeFormatter dateTimeFormatter() {
String formatValue = format();
try {
switch (type) {
case TIME:
return formatValue == null
? ISODateTimeFormat.localTimeParser() : DateTimeFormat.forPattern(formatValue);
case DATE:
formatValue = format();
return formatValue == null
? ISODateTimeFormat.localDateParser() : DateTimeFormat.forPattern(formatValue);
case TIMESTAMP:
formatValue = format();
return formatValue == null
? ISODateTimeFormat.dateTimeNoMillis() : DateTimeFormat.forPattern(formatValue);
default:
throw new IllegalArgumentException("Column is not a date/time type: " + type.toString());
}
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("The format \"%s\" is not valid for type %s",
formatValue, type), e);
}
}
@Override
protected DateTimeFormatter isoFormatter() {
return ISODateTimeFormat.dateTimeNoMillis();
}
public static Date stringToDate(String dateString){
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
return fmt.parseDateTime(dateString).toDate();
}
public static String dateToString(Date date){
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
DateTime dateTime = new DateTime(date);
return fmt.print(dateTime);
}