add date to long support for Convert

This commit is contained in:
Looly 2020-05-17 19:26:40 +08:00
parent 200dc6c5ea
commit a0b1a33654
2 changed files with 40 additions and 27 deletions

View File

@ -31,7 +31,6 @@ import java.util.concurrent.atomic.AtomicLong;
* </ul>
*
* @author Looly
*
*/
public class NumberConverter extends AbstractConverter<Number> {
private static final long serialVersionUID = 1L;
@ -80,6 +79,12 @@ public class NumberConverter extends AbstractConverter<Number> {
return ((Number) value).intValue();
} else if (value instanceof Boolean) {
return BooleanUtil.toInteger((Boolean) value);
} else if (value instanceof Date) {
return (int)((Date) value).getTime();
} else if (value instanceof Calendar) {
return (int)((Calendar) value).getTimeInMillis();
} else if (value instanceof TemporalAccessor) {
return (int)DateUtil.toInstant((TemporalAccessor) value).toEpochMilli();
}
final String valueStr = convertToStr(value);
return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseInt(valueStr);

View File

@ -16,6 +16,14 @@ public class ConvertToNumberTest {
Assert.assertEquals(date.getTime(), dateLong.longValue());
}
@Test
public void dateToIntTest(){
final DateTime date = DateUtil.parse("2020-05-17 12:32:00");
final Integer dateInt = Convert.toInt(date);
assert date != null;
Assert.assertEquals((int)date.getTime(), dateInt.intValue());
}
@Test
public void dateToAtomicLongTest(){
final DateTime date = DateUtil.parse("2020-05-17 12:32:00");