mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
1.修改Convert.toLocalDateTime(Object, LocalDatetime)方法的参数及返回值类型;2.新增一个Convert.toLocalDateTime(Object)方法;
This commit is contained in:
parent
d240756f01
commit
20ffe321f1
@ -468,7 +468,7 @@ public class Convert {
|
||||
* @return 结果
|
||||
* @since 5.0.7
|
||||
*/
|
||||
public static Date toLocalDateTime(Object value, Date defaultValue) {
|
||||
public static LocalDateTime toLocalDateTime(Object value, LocalDateTime defaultValue) {
|
||||
return convertQuietly(LocalDateTime.class, value, defaultValue);
|
||||
}
|
||||
|
||||
@ -499,6 +499,18 @@ public class Convert {
|
||||
return toDate(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为LocalDateTime<br>
|
||||
* 如果给定的值为空,或者转换失败,返回<code>null</code><br>
|
||||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
* @return 结果
|
||||
*/
|
||||
public static LocalDateTime toLocalDateTime(Object value) {
|
||||
return toLocalDateTime(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Enum对象<br>
|
||||
* 如果给定的值为空,或者转换失败,返回默认值<br>
|
||||
|
@ -1,13 +1,15 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
|
||||
public class DateConvertTest {
|
||||
|
||||
@Test
|
||||
@ -51,4 +53,20 @@ public class DateConvertTest {
|
||||
java.sql.Date value2 = Convert.convert(java.sql.Date.class, timeLong);
|
||||
Assert.assertEquals(timeLong, value2.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toLocalDateTimeTest() {
|
||||
Date src = new Date();
|
||||
|
||||
LocalDateTime ldt = Convert.toLocalDateTime(src);
|
||||
Assert.assertEquals(ldt, DateUtil.toLocalDateTime(src));
|
||||
|
||||
Timestamp ts = Timestamp.from(src.toInstant());
|
||||
ldt = Convert.toLocalDateTime(ts);
|
||||
Assert.assertEquals(ldt, DateUtil.toLocalDateTime(src));
|
||||
|
||||
String str = "2020-12-12 12:12:12.0";
|
||||
ldt = Convert.toLocalDateTime(str);
|
||||
Assert.assertEquals(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S")), str);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user