Hello everyone. I'm using DateTime - joda, and the...
# announcements
r
Hello everyone. I'm using DateTime - joda, and the postgresql database. When I send a date, it always records with one less day. Does anyone know why?
d
Mind to share how you stored the date into the database?
r
my class:
Copy code
data class Person (
     @JsonSerialize(using = DateTimeSerializer::class)
     val birthdate: DateTime,
      //more code
)
exposed table
Copy code
object PersonTable : Table("person") {
   val birthdate = date("birthdate")
   //more code
}
and sql
Copy code
CREATE TABLE IF NOT EXISTS person
(
    birthdate   date   not null
    //more code
);
then i send:
482803200000
(1985-04-20) but postgresql writes as:
1985-04-19
i
Maybe timezone?
Try to set both kotlin and postgresql to UTC+0?
t
similar to what @iseki suggested, here’s what we’re doing for all our server apps on startup:
Copy code
DateTimeZone.setDefault(DateTimeZone.UTC) // Joda
val utc = TimeZone.getTimeZone("UTC")
TimeZone.setDefault(utc) // Java, JDBC
k
are you sure it writes as that date? or is it just the way it is displaying because of session timezone?
r
@iseki, @TJ, @kqr You are right is the timezone. Note that in the photo, only the Chinese timezone has the correct value, on the 20th. Now I don't know how to change the Ktor timezone. Could anyone help?
d
when you retrieved the data back from the database, are you getting the correct date?
r
I haven't tested it yet.
d
You can test it first. From what I understand, once retrieved, the library should convert to your current timezone.
r
but what if I need to make a "select" between dates, wouldn't it be a problem?
d
The library should do the conversion for you, just like how it saved the data become UTC+0. Again, that's from what I understand. You might want to test it to verify.
i
Suggest: set all things to UTC+0. Both postgresql and application JVM... And process timezone in your application login
❤️ 1
130 Views