java.sql.Time field off by 30 minutes

#1

Hihi, I'm wondering if this is a bug.

I have a simple entity with java.sql.Time field.

On writing and readback, the field is found to be 30 minutes off. It is observed on objectdb explorer as well.

Timestamp works perfectly fine.

Ps. dont think its a UTC issue. My system is set at far off UTC+30min time.

 

#2

Currently there is no known bug that may cause this. Maybe it is related to your specific system or Java configuration (possibly combined with ObjectDB).

Can you provide a simple runnable example to demonstrate this?

ObjectDB Support
#3

Attached.

 

#4

Thank you for the test case.

Here is a revised version that also prints the default time zone (and with some fixes and adjustments to the posting instructions):

import java.util.*;
import javax.persistence.*;


public class T707 {

    public static void main(String[] args) {
       
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/db/test-my-entity.tmp;drop");
        EntityManager em = emf.createEntityManager();
   
        //()Create/persist
        MyEntity e = new MyEntity();
        em.getTransaction().begin();
        em.persist(e);
        em.getTransaction().commit();
        System.out.println(e);

        //()Refresh read back
        em.refresh(e);
        System.out.println(e);

        //()Read back all
        em.clear(); ///<<<clear local cache and read back new
        Query query = em.createQuery("SELECT e FROM MyEntity e");
        List resultList = query.getResultList();
        System.out.println(resultList);
       
        em.close();
        emf.close();

        System.out.println(TimeZone.getDefault());
    }

    @Entity
    public static class MyEntity {
        @Id @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
        String myname = "Roger";
        java.sql.Time mytime =
            new java.sql.Time(System.currentTimeMillis());
        java.sql.Timestamp mytimestamp =
            new java.sql.Timestamp(System.currentTimeMillis());

        @Override
        public String toString() {
            return "MyEntity{" + "id=" + id + ", myname=" + myname +
                ", mytime=" + mytime + ", mytimestamp=" + mytimestamp + '}';
        }
    }
}

I ran it on my computer and the output looks correct:

MyEntity{id=1, myname=Roger, mytime=21:33:15, mytimestamp=2012-03-07 21:33:15.291}
MyEntity{id=1, myname=Roger, mytime=21:33:15, mytimestamp=2012-03-07 21:33:15.291}
[MyEntity{id=1, myname=Roger, mytime=21:33:15, mytimestamp=2012-03-07 21:33:15.291}]
sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]]

So as expected, this is probably related to specific settings. Maybe there is a problem when the time offset includes half a hour (e.g. UTC+05:30).

Could you please post the output that you get when you run the revised test?

ObjectDB Support
#5

Still the same :(... I am at UTC+8 :

MyEntity{id=1, myname=Roger, mytime=09:07:09, mytimestamp=2012-03-09 9:07:09.66}
MyEntity{id=1, myname=Roger, mytime=08:37:09, mytimestamp=2012-03-09 9:07:09.66}
[MyEntity{id=1, myname=Roger, mytime=08:37:09, mytimestamp=2012-03-09 9:07:09.66}]
sun.util.calendar.ZoneInfo[id="Asia/Singapore",offset=28800000,dstSavings=0,useDaylight=false,transitions=9,lastRule=null]

#6

Since the problem doesn't occur in my environment, exploring it is difficult and your help will be needed.

Here is another simple test (that doesn't use ObjectDB):

public class T707b {

    public static void main(String[] args) {
       
        java.sql.Time mytime1 =
            new java.sql.Time(System.currentTimeMillis());
        System.out.println(mytime1);
        long time = mytime1.getTime();
        System.out.println(time);
        time = time % 86400000;
        java.sql.Time mytime2 = new java.sql.Time(0);
        mytime2.setTime(time);
        System.out.println(mytime2);
    }
}

Could you please run it and publish the output?

ObjectDB Support
#7

1) This is the output:

19:59:27
1331294367749
19:29:27

2) I switched to your location and it works fine. Seems like it doesnt work for my location/UTC.

Other locations with the same UTC+8 seems to work fine though.

Are u able to change the system timezone to my location and test??

I'm using windows 7 home premium and jdk6.

#8

I see the problem on my machine now by running #6 with -Duser.timezone=Asia/Singapore:

22:40:13
1331304013719
22:10:13

But since this test doesn't use ObjectDB, it has to be something in how Java handles this specific time zone.

I found this report that may also be related:

https://stackoverflow.com/questions/7912914/strange-reading-with-java-calendar

As you wrote, the solution may be to use a different UTC+8 time zone.

ObjectDB Support

Reply