mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
add LocalDateTimeUtil
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class LocalDateTimeUtilTest {
|
||||
|
||||
@Test
|
||||
public void nowTest() {
|
||||
Assert.assertNotNull(LocalDateTimeUtil.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest() {
|
||||
String dateStr = "2020-01-23T12:23:56";
|
||||
final DateTime dt = DateUtil.parse(dateStr);
|
||||
|
||||
LocalDateTime of = LocalDateTimeUtil.of(dt);
|
||||
Assert.assertNotNull(of);
|
||||
Assert.assertEquals(dateStr, of.toString());
|
||||
|
||||
of = LocalDateTimeUtil.ofUTC(dt.getTime());
|
||||
Assert.assertEquals(dateStr, of.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56", DateTimeFormatter.ISO_DATE_TIME);
|
||||
Assert.assertEquals("2020-01-23T12:23:56", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23", DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23T00:00", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest3() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("12:23:56", DatePattern.NORM_TIME_PATTERN);
|
||||
Assert.assertEquals("12:23:56", localDateTime.toLocalTime().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest4() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
Assert.assertEquals("2020-01-23T12:23:56", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
String format = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
|
||||
Assert.assertEquals("2020-01-23 12:23:56", format);
|
||||
|
||||
format = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void offset() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
LocalDateTime offset = LocalDateTimeUtil.offset(localDateTime, 1, ChronoUnit.DAYS);
|
||||
// 非同一对象
|
||||
Assert.assertNotSame(localDateTime, offset);
|
||||
|
||||
Assert.assertEquals("2020-01-24T12:23:56", offset.toString());
|
||||
|
||||
offset = LocalDateTimeUtil.offset(localDateTime, -1, ChronoUnit.DAYS);
|
||||
Assert.assertEquals("2020-01-22T12:23:56", offset.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void between() {
|
||||
final Duration between = LocalDateTimeUtil.between(
|
||||
LocalDateTimeUtil.parse("2019-02-02T00:00:00"),
|
||||
LocalDateTimeUtil.parse("2020-02-02T00:00:00"));
|
||||
Assert.assertEquals(365, between.toDays());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beginOfDayTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
final LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(localDateTime);
|
||||
Assert.assertEquals("2020-01-23T00:00", beginOfDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void endOfDayTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
final LocalDateTime endOfDay = LocalDateTimeUtil.endOfDay(localDateTime);
|
||||
Assert.assertEquals("2020-01-23T23:59:59.999999999", endOfDay.toString());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user