Files
plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java

111 lines
4.3 KiB
Java
Raw Normal View History

2024-10-12 00:57:35 +08:00
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
2024-10-09 18:46:18 +08:00
import java.time.format.DateTimeFormatter;
2024-05-28 09:38:59 +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 {
2024-04-03 16:20:41 +08:00
private static final Logger log = LoggerFactory.getLogger(DateTimeToolsTests.class);
@Test
void testLocalNowStr() {
String nowStr = DateTimeTools.nowStr("yyyy/MM/dd HH:mm:ss.SSS");
log.info(nowStr);
assertEquals(23, nowStr.length());
}
@Test
void testToJoda() {
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));
}
@Test
void testToInstant() {
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);
log.info(str);
assertEquals("08-1-8 10:23:50.108", str);
}
@Test
void testToJodaDateTime() {
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
2024-10-09 18:46:18 +08:00
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);
assertEquals("2008-01-08 10:23:50.108", f.print(jodaDateTime));
2024-10-09 18:46:18 +08:00
jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+02:00"));
log.info("jodaDateTime: {}", jodaDateTime);
assertEquals("2008-01-08 04:23:50.108", f.print(jodaDateTime));
}
@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-10-09 18:46:18 +08:00
org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat
.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
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-10-09 18:46:18 +08:00
log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York"))
.toString());
}
2024-08-20 00:42:38 +08:00
@Test
void testToJodaInstant() {
java.time.Instant javaInstant = java.time.Instant.now();
log.info("javaInstant: {}", javaInstant);
org.joda.time.Instant jodaInstant = DateTimeTools.toJodaInstant(javaInstant);
log.info("jodaInstant: {}", jodaInstant);
2024-10-09 18:46:18 +08:00
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTimeFormatter formatter2 = formatter.withZone(ZoneId.systemDefault());
log.info("{}", formatter);
log.info("{}", formatter2);
2024-08-20 00:42:38 +08:00
}
}