Skip to content

Commit

Permalink
Add support for java.sql.Date for IntervalShardingAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Nov 29, 2022
1 parent e71e94d commit 25322ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ private String getDateTimeText(final Comparable<?> endpoint) {
if (endpoint instanceof TemporalAccessor) {
return dateTimeFormatter.format((TemporalAccessor) endpoint);
}
if (endpoint instanceof java.sql.Date) {
return dateTimeFormatter.format(((java.sql.Date) endpoint).toLocalDate().atStartOfDay(ZoneId.systemDefault()));
}
if (endpoint instanceof Date) {
return dateTimeFormatter.format(((Date) endpoint).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.Before;
import org.junit.Test;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -448,4 +449,13 @@ public void assertIntegerInJDBCType() {
new RangeShardingValue<>("t_order", "create_time", DATA_NODE_INFO, Range.closed("04", "10")));
assertThat(actualAsMonthString.size(), is(4));
}

@Test
public void assertDateInSqlDate() {
Collection<String> actualAsLocalDate = shardingAlgorithmByJDBCDate.doSharding(availableTablesForJDBCDateDataSources,
new RangeShardingValue<>("t_order", "create_time", DATA_NODE_INFO,
Range.closed(new Date(LocalDate.of(2021, 6, 15).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()),
new Date(LocalDate.of(2021, 7, 31).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()))));
assertThat(actualAsLocalDate.size(), is(24));
}
}

0 comments on commit 25322ed

Please sign in to comment.