mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
!97 关于jdk8新时间类型LocalDateTime的相关方法
Merge pull request !97 from 27号/v5-dev
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -658,4 +658,20 @@ public class DateUtilTest {
|
||||
boolean expired = DateUtil.isExpired(startDate, DateField.DAY_OF_YEAR, length, endDate);
|
||||
Assert.assertTrue(expired);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localDateTimeTest() {
|
||||
// 测试字符串与LocalDateTime的互相转换
|
||||
String strDate = "2019-12-01 17:02:30";
|
||||
LocalDateTime ldt = DateUtil.parseLocalDateTime(strDate);
|
||||
String strDate1 = DateUtil.formatLocalDateTime(ldt);
|
||||
System.out.println(strDate1);
|
||||
Assert.assertEquals(strDate, strDate1);
|
||||
|
||||
strDate = "2019年12月01日 17:02:30.111";
|
||||
ldt = DateUtil.parseLocalDateTime(strDate, DatePattern.NORM_DATETIME_MS_PATTERN);
|
||||
strDate1 = DateUtil.format(ldt, DatePattern.NORM_DATETIME_MS_PATTERN);
|
||||
System.out.println(strDate1);
|
||||
Assert.assertEquals(strDate, strDate1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user