2023-05-27 04:12:49 +08:00
|
|
|
package xyz.zhouxy.plusone.commons.util;
|
|
|
|
|
|
2024-08-19 18:31:48 +08:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
|
2023-05-27 04:12:49 +08:00
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
2024-08-19 18:31:48 +08:00
|
|
|
import java.time.ZonedDateTime;
|
2024-05-28 09:38:59 +08:00
|
|
|
|
2023-05-27 04:12:49 +08:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2024-04-03 16:20:41 +08:00
|
|
|
class DateTimeToolsTests {
|
2023-05-27 04:12:49 +08:00
|
|
|
|
2024-04-03 16:20:41 +08:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(DateTimeToolsTests.class);
|
2023-05-27 04:12:49 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testLocalNowStr() {
|
2024-08-19 18:31:48 +08:00
|
|
|
String nowStr = DateTimeTools.nowStr("yyyy/MM/dd HH:mm:ss.SSS");
|
|
|
|
|
log.info(nowStr);
|
|
|
|
|
assertEquals(23, nowStr.length());
|
2023-05-27 04:12:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testToJoda() {
|
2024-08-19 18:31:48 +08:00
|
|
|
LocalDateTime dt = LocalDateTime.of(2008, 8, 8, 20, 18, 59, 108000000);
|
|
|
|
|
log.info("src: {}", dt);
|
|
|
|
|
org.joda.time.LocalDateTime dt2 = DateTimeTools.toJodaLocalDateTime(dt);
|
|
|
|
|
log.info("result: {}", dt2);
|
|
|
|
|
org.joda.time.format.DateTimeFormatter f = org.joda.time.format.DateTimeFormat
|
|
|
|
|
.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
|
|
|
|
|
|
|
assertEquals("2008-08-08 20:18:59.108", f.print(dt2));
|
2023-05-27 04:12:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testToInstant() {
|
2024-08-19 18:31:48 +08:00
|
|
|
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
|
|
|
|
|
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
|
|
|
|
|
String str = DateTimeTools.toString("yy-M-d HH:mm:ss.SSS", instant);
|
2023-05-27 04:12:49 +08:00
|
|
|
log.info(str);
|
2024-08-19 18:31:48 +08:00
|
|
|
assertEquals("08-1-8 10:23:50.108", str);
|
2023-05-27 04:12:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testToJodaDateTime() {
|
2024-08-19 18:31:48 +08:00
|
|
|
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
|
|
|
|
|
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
|
|
|
|
|
|
|
|
|
|
org.joda.time.format.DateTimeFormatter f = org.joda.time.format.DateTimeFormat
|
|
|
|
|
.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
|
|
|
|
|
|
|
org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+08:00"));
|
2024-05-28 09:38:59 +08:00
|
|
|
log.info("jodaDateTime: {}", jodaDateTime);
|
2024-08-19 18:31:48 +08:00
|
|
|
assertEquals("2008-01-08 10:23:50.108", f.print(jodaDateTime));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+02:00"));
|
2023-05-27 04:12:49 +08:00
|
|
|
log.info("jodaDateTime: {}", jodaDateTime);
|
2024-08-19 18:31:48 +08:00
|
|
|
assertEquals("2008-01-08 04:23:50.108", f.print(jodaDateTime));
|
2023-05-27 04:12:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void test() {
|
2024-05-28 09:38:59 +08:00
|
|
|
java.time.Instant now = java.time.Instant.now();
|
|
|
|
|
org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York"));
|
2024-08-19 18:31:48 +08:00
|
|
|
org.joda.time.format.DateTimeFormatter formatter =
|
|
|
|
|
org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
2023-05-27 04:12:49 +08:00
|
|
|
log.info(formatter.print(jodaDateTime));
|
|
|
|
|
log.info(jodaDateTime.getZone().toString());
|
|
|
|
|
log.info(jodaDateTime.toString());
|
|
|
|
|
log.info("==========================================");
|
|
|
|
|
org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000);
|
|
|
|
|
log.info(instant.toString());
|
2024-04-03 16:20:41 +08:00
|
|
|
log.info(DateTimeTools.toJavaInstant(instant).toString());
|
2024-05-28 09:38:59 +08:00
|
|
|
log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York")).toString());
|
2023-05-27 04:12:49 +08:00
|
|
|
}
|
|
|
|
|
}
|