Skip to content

Commit 6dc3183

Browse files
committed
Added string to date conversion
1 parent 16d6435 commit 6dc3183

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/main/java/org/bbottema/javareflection/valueconverter/converters/StringConverters.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
import java.io.File;
1616
import java.math.BigDecimal;
1717
import java.math.BigInteger;
18+
import java.text.DateFormat;
19+
import java.text.ParseException;
20+
import java.text.SimpleDateFormat;
1821
import java.util.ArrayList;
1922
import java.util.Collection;
23+
import java.util.Date;
2024
import java.util.UUID;
2125

2226
import static org.slf4j.LoggerFactory.getLogger;
@@ -60,6 +64,7 @@ public final class StringConverters {
6064
converters.add(new ValueFunctionImpl<>(String.class, BigDecimal.class, new StringToBigDecimalFunction()));
6165
converters.add(new ValueFunctionImpl<>(String.class, File.class, new StringToFileFunction()));
6266
converters.add(new ValueFunctionImpl<>(String.class, UUID.class, new StringToUUIDFunction()));
67+
converters.add(new ValueFunctionImpl<>(String.class, Date.class, new StringToDateFunction()));
6368
return converters;
6469
}
6570

@@ -229,4 +234,25 @@ public UUID apply(String value) {
229234
}
230235
}
231236
}
237+
238+
static class StringToDateFunction implements Function<String, Date> {
239+
240+
// Quoted "Z" to indicate UTC, no timezone offset
241+
private static final DateFormat DATETIME_FORMAT_SIMPLE = new SimpleDateFormat("yyyy-MM-dd");
242+
private static final DateFormat DATETIME_FORMAT_ISO8601 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
243+
private IllegalArgumentException INCOMPATIBLE_EXCEPTION = new IllegalArgumentException("not compatible with yyyy-MM-dd or yyyy-MM-dd HH:mm");
244+
245+
@Override
246+
public Date apply(String value) {
247+
try {
248+
return DATETIME_FORMAT_ISO8601.parse(value);
249+
} catch (IllegalArgumentException | ParseException e1) {
250+
try {
251+
return DATETIME_FORMAT_SIMPLE.parse(value);
252+
} catch (IllegalArgumentException | ParseException e2) {
253+
throw new IncompatibleTypeException(value, String.class, Date.class, INCOMPATIBLE_EXCEPTION);
254+
}
255+
}
256+
}
257+
}
232258
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.bbottema.javareflection.valueconverter.converters;
2+
3+
import org.junit.Test;
4+
5+
import java.util.GregorianCalendar;
6+
7+
import static java.util.Calendar.MAY;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.bbottema.javareflection.valueconverter.converters.StringConverters.StringToDateFunction;
10+
11+
public class StringToDateFunctionTest {
12+
13+
@Test
14+
public void testStringToDateConversion() {
15+
final StringToDateFunction converter = new StringToDateFunction();
16+
assertThat(converter.apply("2011-5-4")).isEqualTo(new GregorianCalendar(2011, MAY, 4).getTime());
17+
assertThat(converter.apply("2011-5-14")).isEqualTo(new GregorianCalendar(2011, MAY, 14).getTime());
18+
assertThat(converter.apply("2011-05-4")).isEqualTo(new GregorianCalendar(2011, MAY, 4).getTime());
19+
assertThat(converter.apply("2011-05-14")).isEqualTo(new GregorianCalendar(2011, MAY, 14).getTime());
20+
assertThat(converter.apply("2011-5-4 05:10")).isEqualTo(new GregorianCalendar(2011, MAY, 4, 5, 10).getTime());
21+
assertThat(converter.apply("2011-5-14 05:10")).isEqualTo(new GregorianCalendar(2011, MAY, 14, 5, 10).getTime());
22+
assertThat(converter.apply("2011-05-4 05:10")).isEqualTo(new GregorianCalendar(2011, MAY, 4, 5, 10).getTime());
23+
assertThat(converter.apply("2011-05-14 05:10")).isEqualTo(new GregorianCalendar(2011, MAY, 14, 5, 10).getTime());
24+
assertThat(converter.apply("2011-5-4 5:4")).isEqualTo(new GregorianCalendar(2011, MAY, 4, 5, 4).getTime());
25+
assertThat(converter.apply("2011-5-14 5:4")).isEqualTo(new GregorianCalendar(2011, MAY, 14, 5, 4).getTime());
26+
assertThat(converter.apply("2011-05-4 5:4")).isEqualTo(new GregorianCalendar(2011, MAY, 4, 5, 4).getTime());
27+
assertThat(converter.apply("2011-05-14 5:4")).isEqualTo(new GregorianCalendar(2011, MAY, 14, 5, 4).getTime());
28+
}
29+
}

0 commit comments

Comments
 (0)