diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/RsetDataReader.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/RsetDataReader.java index b85c8e688c..1bd51f91d5 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/RsetDataReader.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/RsetDataReader.java @@ -151,7 +151,7 @@ public String getString() throws SQLException { @Override public Time getTime() throws SQLException { - Calendar cal = dataTimeZone.getTimeZone(); + Calendar cal = dataTimeZone.getDateTimeZone(); if (cal != null) { return rset.getTime(pos(), cal); } else { @@ -161,7 +161,7 @@ public Time getTime() throws SQLException { @Override public Timestamp getTimestamp() throws SQLException { - Calendar cal = dataTimeZone.getTimeZone(); + Calendar cal = dataTimeZone.getDateTimeZone(); if (cal != null) { return rset.getTimestamp(pos(), cal); } else { diff --git a/ebean-test/src/test/java/io/ebeaninternal/server/type/DatesAndTimesTest.java b/ebean-test/src/test/java/io/ebeaninternal/server/type/DatesAndTimesTest.java new file mode 100644 index 0000000000..bd4bcfc1bc --- /dev/null +++ b/ebean-test/src/test/java/io/ebeaninternal/server/type/DatesAndTimesTest.java @@ -0,0 +1,130 @@ +package io.ebeaninternal.server.type; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.tests.model.basic.ModelWithDateTimeProperties; + +import io.ebean.BaseTestCase; +import io.ebean.Database; +import io.ebean.DatabaseFactory; +import io.ebean.config.DatabaseConfig; +import io.ebean.config.dbplatform.mariadb.MariaDbPlatform; +import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform; + +public class DatesAndTimesTest extends BaseTestCase { + + private Database db; + + @BeforeEach + public void setup() { + db = createServer(); + db.find(ModelWithDateTimeProperties.class).delete(); + } + + @AfterEach + public void shutdown() { + db.shutdown(); + } + + private Database createServer() { + + DatabaseConfig config = new DatabaseConfig(); + config.setName("h2other"); + config.loadFromProperties(); + config.setDdlGenerate(true); + config.setDdlRun(true); + config.setDdlExtra(false); + config.setDefaultServer(false); + config.setRegister(false); + config.setChangeLogAsync(false); + + config.setDataTimeZone("GMT"); + + // Mariadb + // config.setDdlCreateOnly(false); + // config.setDdlRun(false); + // config.setName("mariadb-docker04"); + // config.setDatabasePlatform(new MariaDbPlatform()); + // + // config.getDataSourceConfig().setUrl("jdbc:mariadb://srv-01-docker04.foconis.local:3306/zak_szemenyei_1"); + // config.getDataSourceConfig().setDriver("org.mariadb.jdbc.Driver"); + // config.getDataSourceConfig().setUsername("tenant1user"); + // config.getDataSourceConfig().setPassword("tenant1pw"); + // config.getDataSourceConfig().setInitSql(Arrays.asList("SET NAMES utf8mb4", + // "SET collation_connection = 'utf8mb4_bin'")); + + // SqlServer + // config.setDdlCreateOnly(false); + // config.setDdlRun(false); + // config.setName("mssql"); + // config.setDatabasePlatform(new SqlServer17Platform()); + // + // config.getDataSourceConfig().setUrl("jdbc:sqlserver://srv-01-docker02.foconis.local:1433;databaseName=zak_szemenyei_1;sendTimeAsDatetime=false"); + // config.getDataSourceConfig().setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver"); + // config.getDataSourceConfig().setUsername("tenant1user"); + // config.getDataSourceConfig().setPassword("tenant1pw"); + + return DatabaseFactory.create(config); + } + + @Test + public void testLocalTime() { + + String rawSql = "insert into model_with_date_time_properties (id, local_time) values (1, '05:15:15')"; + int count = db.sqlUpdate(rawSql).execute(); + assertEquals(count, 1); + + List list = db.find(ModelWithDateTimeProperties.class).select("localTime").findSingleAttributeList(); + assertEquals(list.size(), 1); + assertEquals(list.get(0).getHour(), 5); + assertEquals(list.get(0).getMinute(), 15); + assertEquals(list.get(0).getSecond(), 15); + } + + @Test + public void testLocalDateTime() { + + Database db = createServer(); + + String rawSql = "insert into model_with_date_time_properties (id, local_date_time) values (1, '2021-11-22 05:15:15')"; + int count = db.sqlUpdate(rawSql).execute(); + assertEquals(count, 1); + + List list = db.find(ModelWithDateTimeProperties.class).select("localDateTime") + .findSingleAttributeList(); + assertEquals(list.size(), 1); + assertEquals(list.get(0).getYear(), 2021); + assertEquals(list.get(0).getMonth(), Month.NOVEMBER); + assertEquals(list.get(0).getDayOfMonth(), 22); + assertEquals(list.get(0).getHour(), 5); + assertEquals(list.get(0).getMinute(), 15); + assertEquals(list.get(0).getSecond(), 15); + } + + @Test + public void testLocalDate() { + + Database db = createServer(); + + String rawSql = "insert into model_with_date_time_properties (id, local_date) values (1, '2021-11-22')"; + int count = db.sqlUpdate(rawSql).execute(); + assertEquals(count, 1); + + List list = db.find(ModelWithDateTimeProperties.class).select("localDate").findSingleAttributeList(); + assertEquals(list.size(), 1); + assertEquals(list.get(0).getYear(), 2021); + assertEquals(list.get(0).getMonth(), Month.NOVEMBER); + assertEquals(list.get(0).getDayOfMonth(), 22); + } + +} diff --git a/ebean-test/src/test/java/org/tests/model/basic/ModelWithDateTimeProperties.java b/ebean-test/src/test/java/org/tests/model/basic/ModelWithDateTimeProperties.java new file mode 100644 index 0000000000..8f52fcbb20 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/model/basic/ModelWithDateTimeProperties.java @@ -0,0 +1,58 @@ +package org.tests.model.basic; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class ModelWithDateTimeProperties{ + + @Id + Integer id; + + @Nullable + LocalTime localTime; + + @Nullable + LocalDateTime localDateTime; + + @Nullable + LocalDate localDate; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public LocalTime getLocalTime() { + return localTime; + } + + public void setLocalTime(LocalTime localTime) { + this.localTime = localTime; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + + public LocalDate getLocalDate() { + return localDate; + } + + public void setLocalDate(LocalDate localDate) { + this.localDate = localDate; + } + +}