diff --git a/persistence-modules/spring-boot-postgresql/pom.xml b/persistence-modules/spring-boot-postgresql/pom.xml index 27c8ae9e5a1b..9a20d6379372 100644 --- a/persistence-modules/spring-boot-postgresql/pom.xml +++ b/persistence-modules/spring-boot-postgresql/pom.xml @@ -41,6 +41,9 @@ org.springframework.boot spring-boot-maven-plugin + + com.baeldung.boot.Application + diff --git a/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/Application.java b/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/Application.java new file mode 100644 index 000000000000..7003ac0393b9 --- /dev/null +++ b/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.postgres.datetime; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValueRepository.java b/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValueRepository.java new file mode 100644 index 000000000000..eca37b843e56 --- /dev/null +++ b/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValueRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.postgres.datetime; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.io.Serializable; + +@Repository +public interface DateTimeValueRepository extends JpaRepository { +} \ No newline at end of file diff --git a/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValues.java b/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValues.java new file mode 100644 index 000000000000..fb0c70b06017 --- /dev/null +++ b/persistence-modules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValues.java @@ -0,0 +1,53 @@ +package com.baeldung.postgres.datetime; + +import jakarta.persistence.*; + +import java.time.*; +import java.util.Date; + +@Entity +public class DateTimeValues { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Integer id; + + private Date date; + private LocalDate localDate; + private LocalDateTime localDateTime; + private Instant instant; + private ZonedDateTime zonedDateTime; + private LocalTime localTime; + private OffsetDateTime offsetDateTime; + private java.sql.Date sqlDate; + + @Column(columnDefinition = "date") + private Instant instantAsDate; + + private String zoneId; + + public DateTimeValues() { + Clock clock = Clock.fixed(Instant.parse("2024-08-01T14:15:00Z"), ZoneId.of("UTC")); + + this.date = new Date(clock.millis()); + this.localDate = LocalDate.now(clock); + this.localDateTime = LocalDateTime.now(clock); + this.zonedDateTime = ZonedDateTime.now(clock); + this.instant = Instant.now(clock); + this.localTime = LocalTime.now(clock); + this.offsetDateTime = OffsetDateTime.now(clock); + this.instantAsDate = Instant.now(clock); + this.sqlDate = java.sql.Date.valueOf(LocalDate.now(clock)); + + this.zoneId = ZoneId.systemDefault().getId(); + } + + public Integer getId() { + return id; + } + + public Instant getInstantAsDate() { + return instantAsDate; + } + +} diff --git a/persistence-modules/spring-boot-postgresql/src/test/java/com/baeldung/postgres/datetime/DateTimeIntegrationTest.java b/persistence-modules/spring-boot-postgresql/src/test/java/com/baeldung/postgres/datetime/DateTimeIntegrationTest.java new file mode 100644 index 000000000000..8d165e2b13a1 --- /dev/null +++ b/persistence-modules/spring-boot-postgresql/src/test/java/com/baeldung/postgres/datetime/DateTimeIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.postgres.datetime; + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.temporal.ChronoUnit; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {Application.class}) +public class DateTimeIntegrationTest { + + @Autowired + private DateTimeValueRepository dateTimeValueRepository; + + @Test + public void givenJavaDateTimesValues_whenPersisted_thenAllValuesAreStored() { + + // Given + DateTimeValues dateTimeValues = new DateTimeValues(); + + // When + dateTimeValues = dateTimeValueRepository.save(dateTimeValues); + DateTimeValues persisted = dateTimeValueRepository.findById(dateTimeValues.getId()).get(); + + // Then + assertNotNull(persisted); + } + + @Test + public void givenJavaInstant_whenPersistedAsSqlDate_thenRetrievedWithoutTime() { + DateTimeValues dateTimeValues = new DateTimeValues(); + DateTimeValues persisted = dateTimeValueRepository.save(dateTimeValues); + DateTimeValues fromDatabase = dateTimeValueRepository.findById(persisted.getId()).get(); + + Assertions.assertNotEquals(dateTimeValues.getInstantAsDate(), fromDatabase.getInstantAsDate()); + Assertions.assertEquals(dateTimeValues.getInstantAsDate().truncatedTo(ChronoUnit.DAYS), fromDatabase.getInstantAsDate()); + } +}