Java Hibernate Query with timezone - java

I need to create a query with my local time (+07:00). The server time is in GMT and all times stored in DB are in GMT too.
For example, today is June 18 I want to find records whose created_at (type: Date) is yesterday in my local time, which means the start date should be 2019-06-16 17:00:00.000 and the end date should be 2019-06-17 16:59:59.999. Is it possible to pass the timezone?

Related

Mariadb day light saving time kotlin

I receive dates as a strings for period 30 october to 31 october when daylight saving is happening.
Relevant data:
[
["2022-10-30T00:00:00+02:00",103.01],
["2022-10-30T00:15:00+02:00",103.01],
["2022-10-30T00:30:00+02:00",103.01],
["2022-10-30T00:45:00+02:00",103.01],
["2022-10-30T01:00:00+02:00",100.49],
["2022-10-30T01:15:00+02:00",100.49],
["2022-10-30T01:30:00+02:00",100.49],
["2022-10-30T01:45:00+02:00",100.49],
["2022-10-30T02:00:00+02:00",100.2],
["2022-10-30T02:15:00+02:00",100.2],
["2022-10-30T02:30:00+02:00",100.2],
["2022-10-30T02:45:00+02:00",100.2],
["2022-10-30T02:00:00+01:00",99.92],
["2022-10-30T02:15:00+01:00",99.92],
["2022-10-30T02:30:00+01:00",99.92]
]
At 2AM they are multiple values but different GMT
I convert the strings to ZoneDateTime with this line:
val zonedTimestamp = ZonedDateTime.parse(timestamp)
.withZoneSameInstant(ZoneId.of("GMT"))
And the values seems to be ok, they are all consecutive, no duplicates for that 1 hour, no GMT added to them
The field timestamp on entity is unique and is of type ZonedDateTime.
In mariadb the field is declared as timestamp (i have used also datetime but is the same problem).
And on saving of the entities i receive the error Duplicate entry '2022-10-30 03:00:00' for key 'timestamp', somehow mariadb is not taking those ZonedDateTime as their values.
I saw suggestions to use timestamp with time zone but is not such a thing in mariadb.

Java : Mysql timestamp daylight saving bug

I have a particular table in mysql with a field refresh_time Type as timestamp.
In my code, I update the refresh_time field to a future date of next month. For that, I calculate the milliseconds for next month date using following logic :
periodRefresh = currentTime + MyServiceUtils.calculateNoOfDaysInMonth(currentTime)*86400000L;
And then the value periodRefresh (milliseconds) I convert to java.sql.Timestamp and pass it to db side to udpate the field :
new Timestamp(periodRefresh);
But what has happened was that the current date in db was 2021-04-03 15:57:13 and when the above logic was run to update the time to next month, same time, it updated the time as 2021-05-03 13:57:13 which is 2 hours less than expected.
Our server follows UTC timezone, but I see that the user was from Australia, and in that day (4th April 2021) there was a daylight savings time in AEST. But even then it doesn't add to above as if I convert 2021-04-03 15:57:13 UTC to AEST, it comes as 2021-04-03 01:57:13 and the daylight savings there is at 3 am.
All the above has confused me a bit with following questions:
If I am following UTC timezones, then also does sql.Timestamp and mysql can get affected by daylight saving time?
How is AEST daylight saving time affecting my Australia based user when I follow UTC timezone?
Even if DST was affecting, so it should have increased/decreased time by 1 hour, but how in my case a difference of 2 hours happened?
In MySQL, TIMESTAMP datatypes are always stored in tables as UTC. When you put them into the database, it first translates them from your connection's time_zone setting to UTC. And when you retrieve them it translates them the other way. This is not true of DATETIME data types.
This TIMESTAMP behavior is handy if you have a global app. You can ask each user for their preferred time zone, and store it as a user-preference setting.
If you take a raw (seconds since the UNIX epoch in UTC) TIMESTAMP value and add a month's worth of seconds to it, the resulting timestamp value may be in a different daylight-time regime. So it will be translated differently.
Either
do your timestamp processing in MySQL, for example with
UPDATE tbl SET periodRefresh = periodRefresh + INTERVAL 1 MONTH;
do it all in your app and use DATETIME, not TIMESTAMP, data types (to avoid the database's time zone translation, or
do it all in your app and use SET time_zone='UTC'; on every connection.

Current time is differed by 6hrs 30min when storing into mongoDB database with Date as Datatype in Java

I've took the Date as the datatype and inserted one record in mongoDB. It is inserted as 2020-04-23T13:41:37.410+00:00 but the current time is 2020-02-23 19:22 (IST). Even if the database in the local also it is inserting the same time.
Please help me with this
MongoDB stores times in UTC by default, and will convert any local time representations into this form.
Below is my system current time
print(new Date() );
Thu Apr 23 2020 19:29:51 GMT+0530 (India Standard Time)
But when I save this in a collection, it will be saved as UTC (ISO date), though it printed new Date in step 1 as IST
db.getCollection("Demo").insert({dat:new Date()})
db.getCollection("Demo").find({})
{ "_id" : ObjectId("5ea1a0d53cd3ffdd3bef987c"), "dat" : ISODate("2020-04-23T14:06:12.564Z") }
First, that's 5.5 hours (not 6.5 hours). And, +00:00 means it is being stored in UTC (not in your local time). The IST offset is
UTC+05:30.

SpringBoot JPA time stored in MySQL in local time zone

I've written a class which looks like this
#Entity
class Employee {
#Column
private String name;
#Column
private Date joinedDate;
}
Now, I assign value to joinedDate by doing new Date(). When I print this date, java will add the timezone correction to this. But Java will internally store time since epoch with no timezone. Let's say the local date was Wed Nov 29 18:43:43 IST 2017
When I store this object into database, what I expected to be stored was the time in GMT/UTC Zero timezone. (or at least, storing the number of milliseconds since epoch). But the value stored is Nov 29 18:43:43.
Which is wrong value. Because it's not storing the UTC time and it's not storing the timezone either. I expect within my application and DB dates should be stored in UTC timezone.
I would like to store UTC value while reading and writing instead of timezone where the application is running. My application could be running in different timezone but using the same database.
How can I achieve this?
I think you must use the preferred type for java 8 java.time.ZonedDateTime that can storage nanosecond precisition and timezone. Also be sure that in your database the type is TIMESTAMP.
You can check out examples in this page: enter link description here
By default java uses the machine timezone for date and calendar, if you want to change it then you have the follow options
Change JVM timezone
java -Duser.timezone=Europe/Sofia com.acme.Main
Create a date instance with a different timezone
Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime()
Use java.time api
Date convertedDatetime = Date.from(datetime.atZone(ZoneId.off("UTC").toInstant());
Obs: your observation is Right, the milliseconds since epoch have not timezone but you are using a date at the database, then it does.
Update
I found that answer , it promises to change only hibernate jdbc charset

Time zone conversion to CST based GMT offset

I have date(01-oct-2014), time (00:37:31), GMT difference(-360) now
I want to get the time conveted to CST. The solution can be in javascript
Or oracle databse.
I have read several articles but could'nt get any where..can some one help me out on this...
In Oracle, to convert your local time to time of another timezone, you need to CAST TIMESTAMP WITH TIMEZONE.
For example, I want to convert 'IST' Indian standard time, i.e. my local timezone to 'CST', i.e. Central :
SQL> WITH T AS
2 ( SELECT to_timestamp('10/01/2014 11','mm/dd/yyyy hh24') ist FROM dual
3 )
4 SELECT ist,
5 CAST(CAST(ist AS TIMESTAMP WITH TIME ZONE) at TIME zone 'CST' AS TIMESTAMP) cst
6 FROM t
7 /
IST CST
----------------------------------- ------------------------------
01-OCT-14 11.00.00.000000000 AM 01-OCT-14 12.30.00.000000 AM
Take care of Daylight saving. You might have to take care in understanding CST and CDT.
There are several ways to do this:
SELECT
(TIMESTAMP '2014-10-01 00:37:31') AT TIME ZONE 'CST',
FROM_TZ((TIMESTAMP '2014-10-01 00:37:31'), 'CST'),
CAST((TIMESTAMP '2014-10-01 00:37:31') AT TIME ZONE 'CST' AS TIMESTAMP)
FROM DUAL;
It depends if the result shall include the new time zone or not.

Categories

Resources