DATACMNS-1440 - Added converters to parse LocalDate(Time) and Instant from their toString() representation.
This commit is contained in:
@@ -26,6 +26,7 @@ import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -36,6 +37,7 @@ import java.util.List;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
@@ -80,6 +82,9 @@ public abstract class Jsr310Converters {
|
||||
converters.add(StringToDurationConverter.INSTANCE);
|
||||
converters.add(PeriodToStringConverter.INSTANCE);
|
||||
converters.add(StringToPeriodConverter.INSTANCE);
|
||||
converters.add(StringToLocalDateConverter.INSTANCE);
|
||||
converters.add(StringToLocalDateTimeConverter.INSTANCE);
|
||||
converters.add(StringToInstantConverter.INSTANCE);
|
||||
|
||||
return converters;
|
||||
}
|
||||
@@ -285,4 +290,52 @@ public abstract class Jsr310Converters {
|
||||
return Period.parse(s);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public static enum StringToLocalDateConverter implements Converter<String, LocalDate> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
@NonNull
|
||||
@Override
|
||||
public LocalDate convert(String source) {
|
||||
return LocalDate.parse(source, DateTimeFormatter.ISO_DATE);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public static enum StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
@NonNull
|
||||
@Override
|
||||
public LocalDateTime convert(String source) {
|
||||
return LocalDateTime.parse(source, DateTimeFormatter.ISO_DATE_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public static enum StringToInstantConverter implements Converter<String, Instant> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
@NonNull
|
||||
@Override
|
||||
public Instant convert(String source) {
|
||||
return Instant.parse(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,6 +156,30 @@ public class Jsr310ConvertersUnitTests {
|
||||
assertThat(convertedDateTime).isEqualTo(dateTime);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1440
|
||||
public void convertsIsoFormattedStringToLocalDate() {
|
||||
|
||||
LocalDate date = LocalDate.now();
|
||||
|
||||
assertThat(CONVERSION_SERVICE.convert(date.toString(), LocalDate.class)).isEqualTo(date);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1440
|
||||
public void convertsIsoFormattedStringToLocalDateTime() {
|
||||
|
||||
LocalDateTime date = LocalDateTime.now();
|
||||
|
||||
assertThat(CONVERSION_SERVICE.convert(date.toString(), LocalDateTime.class)).isEqualTo(date);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1440
|
||||
public void convertsIsoFormattedStringToInstant() {
|
||||
|
||||
Instant date = Instant.now();
|
||||
|
||||
assertThat(CONVERSION_SERVICE.convert(date.toString(), Instant.class)).isEqualTo(date);
|
||||
}
|
||||
|
||||
private static Predicate<Date> formatted(Temporal expected, String format) {
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
@@ -173,7 +197,6 @@ public class Jsr310ConvertersUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public static class DurationConversionTests extends ConversionTest<Duration> {
|
||||
|
||||
// DATACMNS-951
|
||||
@@ -207,7 +230,7 @@ public class Jsr310ConvertersUnitTests {
|
||||
}
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public static class ConversionTest<T> {
|
||||
public static abstract class ConversionTest<T> {
|
||||
|
||||
public @Parameter(0) String string;
|
||||
public @Parameter(1) T target;
|
||||
|
||||
Reference in New Issue
Block a user