fix DateConvert

This commit is contained in:
Looly
2019-12-18 17:34:32 +08:00
parent 7ffdc2c473
commit 0db031e88b
11 changed files with 82 additions and 28 deletions

View File

@@ -216,7 +216,7 @@ public class ConverterRegistry implements Serializable{
if(type instanceof TypeReference) {
type = ((TypeReference<?>)type).getType();
}
// 标准转换器
final Converter<T> converter = getConverter(type, isCustomFirst);
if (null != converter) {

View File

@@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
/**
* 日期转换器
@@ -65,19 +66,25 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
if (value instanceof Calendar) {
// Handle Calendar
mills = ((Calendar) value).getTimeInMillis();
} else if (value instanceof Long) {
// Handle Long
mills = (Long) value;
} else if (value instanceof Number) {
// Handle Number
mills = ((Number) value).longValue();
}else if (value instanceof TemporalAccessor) {
return DateUtil.date((TemporalAccessor) value);
} else {
// 统一按照字符串处理
final String valueStr = convertToStr(value);
Date date = null;
try {
mills = StrUtil.isBlank(this.format) ? DateUtil.parse(valueStr).getTime() : DateUtil.parse(valueStr, this.format).getTime();
date = StrUtil.isBlank(this.format) //
? DateUtil.parse(valueStr) //
: DateUtil.parse(valueStr, this.format);
} catch (Exception e) {
// Ignore Exception
}
if(null != date){
mills = date.getTime();
}
}
if (null == mills) {

View File

@@ -269,6 +269,7 @@ public class DateTime extends Date {
*/
public DateTime offset(DateField datePart, int offset) {
final Calendar cal = toCalendar();
//noinspection MagicConstant
cal.add(datePart.getValue(), offset);
DateTime dt = mutable ? this : ObjectUtil.clone(this);
@@ -286,6 +287,7 @@ public class DateTime extends Date {
*/
public DateTime offsetNew(DateField datePart, int offset) {
final Calendar cal = toCalendar();
//noinspection MagicConstant
cal.add(datePart.getValue(), offset);
DateTime dt = ObjectUtil.clone(this);
@@ -594,6 +596,7 @@ public class DateTime extends Date {
locale = Locale.getDefault(Locale.Category.FORMAT);
}
final Calendar cal = (null != zone) ? Calendar.getInstance(zone, locale) : Calendar.getInstance(locale);
//noinspection MagicConstant
cal.setFirstDayOfWeek(firstDayOfWeek.getValue());
cal.setTime(this);
return cal;

View File

@@ -3,8 +3,6 @@ package cn.hutool.core.convert;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.convert.Convert;
/**
* 类型转换工具单元测试
* 全角半角转换

View File

@@ -21,6 +21,14 @@ public class DateConvertTest {
Assert.assertEquals(timeLong, value2.getTime());
}
@Test
public void toDateFromIntTest() {
int dateLong = -1497600000;
Date value = Convert.toDate(dateLong);
Assert.assertNotNull(value);
Assert.assertEquals("Mon Dec 15 00:00:00 CST 1969", value.toString());
}
@Test
public void toDateFromLocalDateTimeTest() {
LocalDateTime localDateTime = LocalDateTime.parse("2017-05-06T08:30:00", DateTimeFormatter.ISO_DATE_TIME);

View File

@@ -2,6 +2,7 @@ package cn.hutool.core.date;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.BetweenFormater.Level;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@@ -624,6 +625,14 @@ public class DateUtilTest {
Assert.assertEquals("2017-05-06 08:30:00", date.toString());
}
@Test
public void dateTest2(){
// 测试负数日期
long dateLong = -1497600000;
final DateTime date = DateUtil.date(dateLong);
Assert.assertEquals("1969-12-15 00:00:00", date.toString());
}
@Test
public void ageTest(){
String d1 = "2000-02-29";