Hibernate does not provide date in UTC timezone

This seems like a silly question, but I can't understand this creepy behavior. I am fully aware of the fact that the java class Datedoes not store any TimeZone information in it. It just stores the number of milliseconds since January 1, 1970, 00:00:00 GMT

The fact is that I use MySql, which is located on a server with a UTC time zone, and I also save DateTimeonly in UTC format. If I make a selection request, I get this date2014-01-17 16:15:49

Using http://www.epochconverter.com/ , I get the following:

Epoch timestamp: 1389975349
Timestamp in milliseconds: 1389975349000
Human time (GMT): Fri, 17 Jan 2014 16:15:49 GMT
Human time (your time zone): Friday, January 17, 2014 9:45:49 PM

Now comes the Hibernate part. I am running my Java web application on a machine with IST as the system time zone. I made a simple selection of the object using Id and the extracted property createdDate, which is the object Date. I wrote simple code to understand its output, here is the code:

Date dt = c.getCreatedDate();
System.out.println(dt.getTime());
System.out.println(dt);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy  hh:mm a z");
df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(df.format(dt));
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(df.format(dt));

And here is the result for this:

1389955549000
2014-01-17 16:15:49.0
17/01/2014  04:15 PM IST
17/01/2014  10:45 AM UTC

If you put this one 1389955549000at http://www.epochconverter.com/ , you will get the following result:

GMT: Fri, 17 Jan 2014 10:45:49 GMT
Your time zone: Friday, January 17, 2014 4:15:49 PM GMT+5.5

This is not an expected result, right. This gives me a millise time that is -5: 30 of UTC time, so if I try to get the time in the IST time zone, then it actually gives me a time that is in UTC

Has anyone understood what I'm doing wrong?

-------------------------- How I fixed it ---------- ---------- --------

Ako

/ . DateTime UTC , , , Java- (, IST - ), hibernate , DateTime IST, .

Basil, . UTC . , , , :

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

ServletContextListener, UTC, hibernate .

+4
2

.

If I make a select query - .

( , -), , . , MySQL , .

, MySQL, . ... google " utc".

, : , .

, , . , "" " " " ".

, . +05: 30 "/", "/" . 5,5 UTC/GMT.

Joda

java.util.Date Calendar, , , . . Joda-Time, Java 8 java.time. * classes ( Joda-Time).

Joda-Time 2.3 Java 8 Mac .

Baseline

, 1389975349000L ≈ 16:15 UTC ≈ 21:45 .

EpochConverter.com, .

// Specify a time zone rather than depend on defaults.
DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );

long millis = 1389975349000L;
DateTime dateTimeUtc = new DateTime( millis, DateTimeZone.UTC );
DateTime dateTimeKolkata = dateTimeUtc.toDateTime( timeZoneKolkata );

...

System.out.println( "millis: " + millis );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeKolkata: " + dateTimeKolkata );

...

millis: 1389975349000
dateTimeUtc: 2014-01-17T16:15:49.000Z
dateTimeKolkata: 2014-01-17T21:45:49.000+05:30

: 1389955549000L.

, , .

, 1389955549000L ≈ 10:45 UTC ≈ 16:15 .

long mysteryMillis = 1389955549000L;
DateTime mysteryUtc = new DateTime( mysteryMillis, DateTimeZone.UTC );
DateTime mysteryKolkata = mysteryUtc.toDateTime( timeZoneKolkata );

...

System.out.println( "mysteryMillis: " + mysteryMillis );
System.out.println( "mysteryUtc: " + mysteryUtc );
System.out.println( "mysteryKolkata: " + mysteryKolkata );

...

mysteryMillis: 1389955549000
mysteryUtc: 2014-01-17T10:45:49.000Z
mysteryKolkata: 2014-01-17T16:15:49.000+05:30

100%, ...

→ -, , , UTC, .

-, -, 16:15 , , -, 21:45. , .

UTC UTC = WRONG.

, .

, "16:15" .

java.util.Date , Java Virtual Machine toString. , .

java.util.Date, java.util.Calendar java.text.SimpleDateFormat. .

Joda-Time java.time. * (JSR 310).

, .

. Verify by googling " utc".

UTC GMT, . , "/" , UTC/GMT - .

JVM TimeZone.setDefault ( ). , , JVM. , . . JVM . Joda-Time, java.time , . , OS UTC, , Java.

+4

; JVM UTC ( , , JVM Java-, ).

, DbAssist, . java.util.Date UtcDateType. JDBC ( Hibernate) UTC. Hibernate ( API ), .

. , , , Hibernate 5.2.2, POM:

<dependency>
    <groupId>com.montrosesoftware</groupId>
    <artifactId>DbAssist-5.2.2</artifactId>
    <version>1.0-RELEASE</version>
</dependency>

JPA Annotations HBM , ; JPA Annotations ( Spring Boot) persistence.xml <persistence-unit>:

<class>com.montrosesoftware.dbassist.types</class>

UTC, / . Java/Hibernate, .

+1

All Articles