This commit is contained in:
Looly
2024-07-03 02:00:25 +08:00
parent aa8d66b81a
commit 71a59b645b
16 changed files with 151 additions and 139 deletions

View File

@@ -12,6 +12,7 @@
package org.dromara.hutool.core.date;
import org.dromara.hutool.core.lang.Console;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -40,4 +41,16 @@ public class CalendarUtilTest {
DateUtil.date(calendar);
});
}
@Test
void setTimeZoneTest() {
// Calendar中设置时区并不会变化时间戳
final Calendar instance = Calendar.getInstance();
Console.log(instance.getTimeInMillis());
final long timeInMillis = instance.getTimeInMillis();
instance.setTimeZone(ZoneUtil.ZONE_UTC);
final long timeInMillis2 = instance.getTimeInMillis();
Assertions.assertEquals(timeInMillis, timeInMillis2);
}
}

View File

@@ -16,7 +16,7 @@ public class DateBuilderTest {
builder.setDay(1);
final Date date = builder.toDate();
Assertions.assertEquals("2019-10-01", DateUtil.date(date).toDateStr());
Assertions.assertEquals("2019-10-01 00:00:00", date.toString());
}
@Test
@@ -32,7 +32,7 @@ public class DateBuilderTest {
.setZone(TimeZone.getDefault());
final LocalDateTime dateTime = builder.toLocalDateTime();
Assertions.assertEquals("2019-10-01T10:20:30.900", builder.toLocalDateTime().toString());
Assertions.assertEquals("2019-10-01T10:20:30.900", dateTime.toString());
}
@Test

View File

@@ -15,6 +15,8 @@ package org.dromara.hutool.core.date;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.TimeZone;
/**
* DateTime单元测试
*
@@ -128,6 +130,17 @@ public class DateTimeTest {
Assertions.assertEquals("2017-01-05T12:34:23+08:00", dateStr);
}
@Test
public void toStringWithTimeZoneTest() {
final DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
final String dateStr = dateTime.toString(TimeZone.getTimeZone("UTC"));
Assertions.assertEquals("2017-01-05 04:34:23", dateStr);
dateTime.setTimeZone(TimeZone.getTimeZone("UTC"));
Assertions.assertEquals("2017-01-05 04:34:23", dateTime.toString());
}
@Test
public void monthTest() {
//noinspection ConstantConditions
@@ -138,7 +151,7 @@ public class DateTimeTest {
@Test
public void weekOfYearTest() {
final DateTime date = DateUtil.date(DateUtil.parse("2016-12-27"));
//noinspection ConstantConditions
Assertions.assertEquals(2016, date.year());
//跨年的周返回的总是1
Assertions.assertEquals(1, date.weekOfYear());

View File

@@ -18,7 +18,7 @@ public class IssueI9C2D4Test {
public void parseHttpTest2() {
final String dateStr = "星期四, 28 三月 2024 14:33:49 GMT";
final Date parse = DateUtil.parse(dateStr);
Assertions.assertEquals("2024-03-28 14:33:49", Objects.requireNonNull(parse).toString());
Assertions.assertEquals("2024-03-28 22:33:49", Objects.requireNonNull(parse).toString());
}
@Test

View File

@@ -77,5 +77,14 @@ public class MonthTest {
month = Month.of(java.time.Month.FEBRUARY);
Assertions.assertEquals(Month.FEBRUARY, month);
month = Month.of("二月");
Assertions.assertEquals(Month.FEBRUARY, month);
month = Month.of("十月");
Assertions.assertEquals(Month.OCTOBER, month);
month = Month.of("十一月");
Assertions.assertEquals(Month.NOVEMBER, month);
month = Month.of("十二月");
Assertions.assertEquals(Month.DECEMBER, month);
}
}

View File

@@ -19,7 +19,6 @@ import org.junit.jupiter.api.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@@ -37,7 +36,8 @@ public class TimeUtilTest {
@Test
public void ofTest() {
final String dateStr = "2020-01-23T12:23:56";
final Date dt = DateUtil.parse(dateStr);
final DateTime dt = DateUtil.parse(dateStr);
Console.log(dt.getTimeZone());
final LocalDateTime of = TimeUtil.of(dt);
Assertions.assertNotNull(of);
@@ -47,9 +47,10 @@ public class TimeUtilTest {
@Test
public void ofUTCTest() {
final String dateStr = "2020-01-23T12:23:56Z";
final Date dt = DateUtil.parse(dateStr);
// 因为`Z`位于末尾表示为UTC时间parse后会自动转换为本地时间
final DateTime dt = DateUtil.parse(dateStr);
final LocalDateTime of = TimeUtil.of(dt);
final LocalDateTime of = TimeUtil.of(dt.setTimeZone(ZoneUtil.ZONE_UTC));
final LocalDateTime of2 = TimeUtil.ofUTC(dt.getTime());
Assertions.assertNotNull(of);
Assertions.assertNotNull(of2);

View File

@@ -19,7 +19,8 @@ import java.time.ZoneId;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class ZoneUtilTest {
@@ -55,21 +56,6 @@ public class ZoneUtilTest {
assertNull(result, "Expected null TimeZone for invalid offset");
}
@Test
public void testGetTimeZoneByOffsetWithNullTimeUnitThrowsException() {
// Arrange
final int rawOffset = 8;
final TimeUnit timeUnit = null; // Null unit to simulate error condition
// Act & Assert
final NullPointerException thrown = assertThrows(
NullPointerException.class,
() -> ZoneUtil.getTimeZoneByOffset(rawOffset, timeUnit),
"Expected NullPointerException for null TimeUnit"
);
assertTrue(thrown.getMessage().contains("timeUnit"), "Exception message should mention the null parameter");
}
@Test
public void testGetAvailableIDWithInvalidOffset() {
// Test with an invalid offset that should result in null or an exception.

View File

@@ -63,6 +63,8 @@ public class GlobalRegexDateParserTest {
assertParse("2014-04-08 00:00:00", "2014年04月08日");
assertParse("2017-02-01 12:23:45", "2017年02月01日 12时23分45秒");
assertParse("2017-02-01 12:23:45", "2017年02月01日 12:23:45");
assertParse("2024-03-28 22:33:49", "星期四, 28 三月 2024 14:33:49 GMT");
assertParse("2024-03-28 22:33:49", "周四, 28 三月 2024 14:33:49 GMT");
}
@Test