mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix DateConvert
This commit is contained in:
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
@@ -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;
|
||||
|
@@ -3,8 +3,6 @@ package cn.hutool.core.convert;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
|
||||
/**
|
||||
* 类型转换工具单元测试
|
||||
* 全角半角转换
|
||||
|
@@ -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);
|
||||
|
@@ -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";
|
||||
|
Reference in New Issue
Block a user