[util-sql-date-improvement] add sqlDate -> utilDate (#15578)

* [util-sql-date-improvement] add sqlDate -> utilDate

* [util-sql-date-improvement] clean up imports
This commit is contained in:
Kai Yuan 2024-01-13 02:21:05 +01:00 committed by GitHub
parent e2e9b10e68
commit a6222f4432
1 changed files with 30 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.text.ParseException;
import java.util.Date;
public class UtilToSqlDateUtilsUnitTest {
@ -25,7 +26,7 @@ public class UtilToSqlDateUtilsUnitTest {
UtilToSqlDateUtils.switchTimezone("Rome");
sqlDate = new java.sql.Date(date.getTime());
Assertions.assertEquals("2010-05-24",sqlDate.toString());
Assertions.assertEquals("2010-05-24", sqlDate.toString());
}
@Test
@ -33,7 +34,7 @@ public class UtilToSqlDateUtilsUnitTest {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T22:01:02");
UtilToSqlDateUtils.switchTimezone("America/Los_Angeles");
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
Assertions.assertEquals("2010-05-23 22:01:02.0",timestamp.toString());
Assertions.assertEquals("2010-05-23 22:01:02.0", timestamp.toString());
}
@Test
@ -42,12 +43,36 @@ public class UtilToSqlDateUtilsUnitTest {
UtilToSqlDateUtils.switchTimezone("America/Los_Angeles");
java.time.LocalDate localDate = UtilToSqlDateUtils.getLocalDate(date,"America/Los_Angeles");
java.time.LocalDate localDate = UtilToSqlDateUtils.getLocalDate(date, "America/Los_Angeles");
Assertions.assertEquals(localDate.toString(), "2010-05-23");
UtilToSqlDateUtils.switchTimezone("Rome");
localDate = UtilToSqlDateUtils.getLocalDate(date,"America/Los_Angeles");
localDate = UtilToSqlDateUtils.getLocalDate(date, "America/Los_Angeles");
Assertions.assertEquals(localDate.toString(), "2010-05-23");
}
}
@Test
public void givenSqlDateAndTimestamp_whenUsingAsUtilDate_thenGetExpectedResult() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T00:00:00");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Assertions.assertEquals(date, sqlDate);
java.util.Date dateWithTime = UtilToSqlDateUtils.createAmericanDate("2010-05-23T23:59:59");
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(dateWithTime.getTime());
Assertions.assertEquals(dateWithTime, sqlTimestamp);
}
@Test
public void givenSqlDateAndTimestamp_whenCreatingNewUtilDate_thenGetExpectedResult() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T00:00:00");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
java.util.Date newDate = new Date(sqlDate.getTime());
Assertions.assertEquals(date, newDate);
java.util.Date dateWithTime = UtilToSqlDateUtils.createAmericanDate("2010-05-23T23:59:59");
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(dateWithTime.getTime());
java.util.Date newDateWithTime = new Date(sqlTimestamp.getTime());
Assertions.assertEquals(dateWithTime, newDateWithTime);
}
}