Spring Boot Query on Date field without time - spring-boot

I have an entity with a Date field with type of java.util.Date, and this field in oracle db has the timestamp type.
the problem is that when i use the repository to find by given specs, it returns nothing for the given date.
It only happens when i want the exact equal date. it works fine with greaterThen and lessThen and that's because the time of the saved records are different.
how could I ignore the time of records and fetch them only for the given date?
Thanks a million

I think you need to look at #Temporal #JsonFormat annotations. Link
Sample usage;
#Temporal(TemporalType.TIMESTAMP)
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MMdd'T'HH:mm:ss")
protected Date lastUpdate;

because the data was saved with the time best way was to do it with between condition from 00:00 to 23:59 in that day. special thanks to this link :
Spring Data Querying DateTime with only Date
but i solved the problem by creating a view from that table and removing the time from the date in oracle.

Related

Convert TimeStamp To Joda DateTime Without Any Timezone Conversion

I have timestamp value in Oracle database column stored in UTC. I want to read it through spring's jdbcTemplate and convert it to joda DateTime object without any timezone conversion i.e. read it as is without converting or losing timezone.
For e.g. if the input timestamp is 2019-03-08 15:07:37.232, I would like to have the DateTime object with the value 2019-03-08T15:07:37.232Z
How can I achieve this?
Note this code - new DateTime(timestamp.getTime(), DateTimeZone.UTC)) does not help since it assumes that the input timestamp is in local timezone and reconverts it to UTC. For the above input is 2019-03-08 15:07:37.232 the outcome comes to 2019-03-08T09:37:37.232Z
Thanks.
One solution that worked was to change the way the timestamp column value is retrieved from the database. The below snippet works for converting a timestamp column value to joda DateTime without losing/converting the timezone
new DateTime(resultSet.getTimestamp("CreatedDateTime",
Calendar.getInstance(TimeZone.getTimeZone("UTC"))), DateTimeZone.UTC)
Hope that helps others too.
If you already have a DateTime object there is method
public DateTime withZone(DateTimeZone newZone)
which would return a copy of datetime with a different time zone, preserving the millisecond instant
eg:- dateTime.withZone(DateTimeZone.UTC)

Laravel - What's the difference between timestamp() and timestampTz()?

What's the difference between timestamps() and timestampsTz() methods in the Laravel's Schema Builder? I tried searching google but couldn't find any help.
Basicly timestampsTz() stands for Timestamp with Timezone while timestamps() is like Timestamp without timezone
TimestampsTz() is a representation for a specific point in time. The adjustment to this time is made by the timezone that is related to your system.
The normal timestamps() is more a representation like normal clock.
Have a look at this example:
If you are using the timestamps() function you will also store the timezone that was chosen by your environment. If you are using the timestampsTz() you will not safe the timezone.
I guess it would be good practice to use timestamp without timezone when all your data is for sure in the same zone, or you got another layer over there which handles the time conversion.
Update:
In the Database the normal timestamps() will look like
2004-10-19 10:23:54
while the timestampsTz() looks like
2004-10-19 10:23:54+02
Update 2:
These functions are in Laravel available for every type of Database. But this only differs for PostgreSQL. Have a look at the docs: PostgreSQL Timestamp. In the other databases the timezone information is included in the timestamp automatically.
In all other databases this will have the same output.
timestampsTz() = Adds nullable created_at and updated_at TIMESTAMP (with timezone) equivalent columns.
timestamps() = Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
The difference is that timestampsTz() adds a timezone.

Spring Hibernate JPA Java8 OffsetTime

I have created an Hibernate Entity which has a OffsetTime column. While saving a time value with time offset e.g (+05:00), it discards time offset, and saves the time with the time offset according to the system timezone. Here is my column in entity:
#Column(name = "start_time", nullable = false)
private OffsetTime startTime;
Then if I try to save "12:15+01:00". It save "12:15+00:00", if my machine is in UTC timezone, if my machine is in IST timezone, it save "12:15+05:00". I want it to save "12:15+01:00" irrespective of machine's timezone.
Please suggest what I need to correct/review to ensure I fix this issue.
Thanks.
Question is how do you do the setting of the value. You didn't show your setter method. You either parse it from String or operate some instance of Temporal that you convert to OffsetTime in either case you will need to provide the desired offset if you don't want to use the default one. Say that you want to save current time with given offset. So you will have to do this:
OffsetTime startTime = OffsetTime.of(LocalTime.now, ZoneOffset.of("+05:00");
See method of() as well of other methods of OffsetTime

Jodas LocalDateTime with Spring Data and Couchbase has no timezone

I wanna persistant an object with LocalDateTime fields with spring data and a couchbase behind in a spring boot app.
Here are the fieldmapping:
#Field
private LocalDateTime start;
#Field
private LocalDateTime end;
When I save the object then the dates are stored as numbers in couchbase.
Here are the stored data in couchbase:
"start": 1518818215508,
So the problem is, if I store an LocalDateTime e.g at 10.00, and then read it from db the result is 09:00 instead of 10:00 because of my local time +1:00.
In Postgres I would save the date in an column with mapping: columnDefinition= "TIMESTAMP WITH TIME ZONE"
How I can solve this problem in couchbase?
JSON (famously) has no date format (unlike relational databases which have a sprawling number of different formats). If you want to store timezone data in JSON, here are two options I can think of:
Using a string instead of a number, and then using something like ISO-8601.
Store the epoch time as you currently are as a UTC value, but also store a separate number field that represents a timezone offset from UTC
I would recommend going with the first approach.
I'm no Spring/Java expert, but a quick look at the documentation says you can do this by setting system property org.springframework.data.couchbase.useISOStringConverterForDate to true

Updating date field using Hibernate - Row not updated if date is the same, time is different

I have a situation where a table in the database has a date field defined as date where time also is important (for sorting later).
At first, all times for the date where coming as 000000 but I updated the code to use timestamp and when inserting new records, it's working fine.
Update on the other hand will not change the database if the date is the same (but different time). Apparently, while inserting, hibernate doesn't take into consideration the time and the record is not change (or at least this is what I discovered from my testing).
I can't change the database structure to use timestamp or add a time field.
Any help is really appreciated :)
Thanks

Resources