forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bael 7989 storing date time in postgres (eugenp#18006)
* 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
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ules/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValueRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
53 changes: 53 additions & 0 deletions
53
...s/spring-boot-postgresql/src/main/java/com/baeldung/postgres/datetime/DateTimeValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...boot-postgresql/src/test/java/com/baeldung/postgres/datetime/DateTimeIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |