简体   繁体   中英

IBATIS TypeHandlerCallback getResult(ResultGetter getter) for converting EST time to UTC time

Thanks for the solution,but its not working out for me. I am dealing with a scenario where I am setting date(with time and timezone information) in the oracle database.I use Ibatis to extract this date and assign it to a java Date object. I implemented my TypeHandlerCallback as follows:

public class DateTimezoneTypeHandler implements TypeHandlerCallback {

public void setParameter(ParameterSetter setter, Object parameter) throws SQLException

{ java.util.Date date = (java.util.Date) parameter; if ( date == null ) setter.setNull(Types.TIMESTAMP); else { Timestamp timestamp = new Timestamp(date.getTime()); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); setter.setTimestamp(timestamp, calendar); } }

@Override public Object getResult(ResultGetter getter) throws SQLException {

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

return getter.getTimestamp(calendar);

}

@Override public Object valueOf(String s) { throw new UnsupportedOperationException( "DateTimezoneTypeHandler.valueOf() is not supported."); } }

I have stored my date in the database in EST timezone and so getter has the date in EST time zone.Now when the date is read from the database,getResult function gets called but EST date is not not getting converted to UTC/GMT time zone. It is converting date having EST timezone to date having my local system timezone

**public Object getResult(ResultGetter getter) throws SQLException {

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

return getter.getTimestamp(calendar);

}**

Following is my sql mapping:

I am not understanding what is the issue with this method implementation and why its not required functionality.

Please let me know if anyone has any suggestion/solution about this issue. I will really appreciate it.

The problem appears to be that you are not actually translating the time. Setting the timezone does not cause Calendar to convert the current value to the new timezone. Here is some example code that performs timezone translation GMT-5 (aka EST) to UTC. Note that I use Calendar.add and not Calendar.roll .


    public static void main(String[] args)
    {
        Calendar calendar = Calendar.getInstance();
        Date date;
        int rawOffset;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d hh:mm;ss a");
        TimeZone timeZone;

        date = calendar.getTime();
        System.out.println(simpleDateFormat.format(date));

        timeZone = TimeZone.getTimeZone("GMT-5");
        rawOffset = timeZone.getRawOffset();
        System.out.println(rawOffset);
        calendar.add(Calendar.MILLISECOND, rawOffset);

        date = calendar.getTime();
        System.out.println(simpleDateFormat.format(date));
    }

FWIW, our company has stopped storing Dates and Times as an Oracle timezone in the database. There is too much dependency on external configuration. Instead, we do the following:

  1. Since asking for currentTimeMillis returns an absolute point in time , we create a TypeHandler for the Java Date class which stores the date/time in a NUMBER column in the database
  2. Because reading millis-from-epoch is hard for humans, we add 2 functions to the database to convert from/to millis so that administrators can still write queries if they like to

The advantage of this is that you circumvent any "smart" time handling by anything outside the JVM. This is particularly handy when people enter a date in a foreign browser, pass that into your server app, and then passing it into the database.

In case you do want to store Dates in the database (sometimes you have to do these things), I personally can recommend Joda-Time for easier handling of dates, times, gregorian calendars and the likes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM