Skip to content

Commit

Permalink
Bael 7989 storing date time in postgres (eugenp#18006)
Browse files Browse the repository at this point in the history
* BAEL-7989 Storing date and time in PostgreSQL

* BAEL-7989 Fix format

* BAEL-7989 Update pom to fix build

* BAEL-7989 Update UCT to UTC

* BAEL-7989 Update integration test

* BAEL-7989 Update test method names

---------

Co-authored-by: michaelk <[email protected]>
  • Loading branch information
Maiklins and michaelk authored Dec 2, 2024
1 parent 304183e commit 92480f7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions persistence-modules/spring-boot-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.boot.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<DateTimeValues, Serializable> {
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 92480f7

Please sign in to comment.