googytech
googytech

Reputation: 479

Java Entity Hibernate to Oracle Timezone conversion issue

My DB is running in ET time. My java application server is running in PT timezone.

Application code --- entitybean.setdate(Util.converCurrentDateToTimeZoneDate("ET"));

   public static Date converCurrentDateToTimeZoneDate(String timeZone){

    Calendar calInput = new GregorianCalendar();
    Calendar calOutput = new GregorianCalendar();

    if (timeZone.equals("ET")){
        calInput.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    }

    if (timeZone.equals("PT")){
        calInput.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
    }
    if (timeZone.equals("CT")){
        calInput.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
    }

    if (timeZone.equals("MT")){
        calInput.setTimeZone(TimeZone.getTimeZone("America/Denver"));
    }

    calOutput.setTimeZone(calInput.getTimeZone());
    return calOutput.getTime();

}

The scenario is :

If my server time is 1:00 PM PT time, the util is converting it to 4:00PM ET correctly, which is the value which i want to store in table.

But when I save the entity the value that is getting reflected in table is 4:00 PM PT.

Please help in resolving this issue ?

Upvotes: 0

Views: 308

Answers (1)

venusoft
venusoft

Reputation: 150

I'm not sure I got your point but I hope a comment about my experience could help you in finding the correct answer. Assuming that you are using an Oracle DATE type, to the best of my knowledge, it doesn't have any timezone information in it (as Java DATE, it stores a time difference in ms). You deal with timezones only when you display or represent a date.

If you keep this in mind you can ignore timezones on db side of your application and focus on the Java client; for example if you can consider all dates in PT timezone, store them as dates on database, and represent them with the timezone you need only when you have to diplay them.

Please consider this link for further reading about this issue:

How to store date/time and timestamps in UTC time zone with JPA and Hibernate

Hope this helps.

Upvotes: 1

Related Questions