BAEL-5790 - Convert java.util.Date to java.sql.Date (#12917)

* BAEL-5790 - Convert java.util.Date to java.sql.Date

* BAEL-5790 - Renamed unit test

* BAEL-5790 | fixed test

* Update core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/utiltosqldate/UnitToSqlDateUtils.java

Co-authored-by: Brandon Ward <brandon.ward1993@gmail.com>

* Update core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/utiltosqldate/UtilToSqlDateUtilsUnitTest.java

Co-authored-by: Brandon Ward <brandon.ward1993@gmail.com>

* Update core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/utiltosqldate/UtilToSqlDateUtilsUnitTest.java

Co-authored-by: Brandon Ward <brandon.ward1993@gmail.com>

* BAEL-5790 | renamed class

* BAEL-5790 | spaces

* BAEL-5790 | renaming class

* Update core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/utiltosqldate/UtilToSqlDateUtilsUnitTest.java

Co-authored-by: KevinGilmore <kpg102@gmail.com>

* Update core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/utiltosqldate/UtilToSqlDateUtilsUnitTest.java

Co-authored-by: KevinGilmore <kpg102@gmail.com>

* Update core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/utiltosqldate/UtilToSqlDateUtilsUnitTest.java

Co-authored-by: KevinGilmore <kpg102@gmail.com>

* Update core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/utiltosqldate/UtilToSqlDateUtilsUnitTest.java

Co-authored-by: KevinGilmore <kpg102@gmail.com>

* BAEL-5790 - renamed unit test

Co-authored-by: Brandon Ward <brandon.ward1993@gmail.com>
Co-authored-by: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
Gaetano Piazzolla 2022-11-07 18:40:22 +01:00 committed by GitHub
parent 0a40935b0f
commit d0c3a7dfd6
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.utiltosqldate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.TimeZone;
public class UtilToSqlDateUtils {
public static java.util.Date createAmericanDate(String date) throws ParseException {
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
return isoFormat.parse(date);
}
public static void switchTimezone(String timeZone) {
TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
}
public static LocalDate getLocalDate(java.util.Date date, String timeZone) {
return date.toInstant().atZone(ZoneId.of(timeZone)).toLocalDate();
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.utiltosqldate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.text.ParseException;
public class UtilToSqlDateUtilsUnitTest {
@Test
public void givenUtilDate_whenCastingToSqlDate_thenThrowException() {
Assertions.assertThrows(ClassCastException.class, () -> {
java.sql.Date date = (java.sql.Date) new java.util.Date();
});
}
@Test
public void givenUtilDate_whenStandardConversion_thenTimezoneLost() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T22:01:02");
UtilToSqlDateUtils.switchTimezone("America/Los_Angeles");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Assertions.assertEquals("2010-05-23", sqlDate.toString());
UtilToSqlDateUtils.switchTimezone("Rome");
sqlDate = new java.sql.Date(date.getTime());
Assertions.assertEquals("2010-05-24",sqlDate.toString());
}
@Test
public void givenUtilDate_whenConversionToTimestamp_thenKeepTimeInfo() throws ParseException {
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());
}
@Test
public void givenUtilDate_whenUsingJavaTimeConversion_thenTimezoneKept() throws ParseException {
java.util.Date date = UtilToSqlDateUtils.createAmericanDate("2010-05-23T22:01:02");
UtilToSqlDateUtils.switchTimezone("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");
Assertions.assertEquals(localDate.toString(), "2010-05-23");
}
}