English

Problem: ArcSDE Java API: Dates inserted into and read from ArcSDE are incorrect

Description

Inserting dates into an ArcSDE 8.2 server using the Java API SeRow.setDate() method produces incorrect results. When the dates that were inserted using the Java API are read directly from the database using a SQL editor or ArcGIS, each date value is offset by a fixed value. The same offset in date values is also observed when reading dates from ArcSDE.

Cause

The Java API handles all date objects with the java.util.TimeZone component set to GMT. If the TimeZone component of a date object passed to the SeRow.setDate() method is not GMT, the time is changed to its GMT value. By default, any Date or Calendar object uses the TimeZone setting of the PC on which the JVM is running.

Solution or Workaround

Install ArcSDE 8.3 or higher.

You can work around the insert problem by modifying the TimeZone component of the date object before it is inserted into ArcSDE.


Code:
/*
* Code before work-around
*/
SeRow row = insert.getRowToSet();

row.setDate(columnPosition, dateObject);

/*
* Code with changes to work around problem
*/
SeRow row = insert.getRowToSet();

int year = dateObject.getYear()+1900;
int month = dateObject.getMonth();
int date = dateObject.getDate();
int hour = dateObject.getHours();
int min = dateObject.getMinutes();
int sec = dateObject.getSeconds();

Calendar cal = Calendar.getInstance();
cal.setTimeZone( TimeZone.getTimeZone("GMT") );
cal.set(year, month, date, hour, min, sec);

row.setDate(columnPosition, cal.getTime() );

You can work around the problem observed during the read operation by modifying the TimeZone component of the date object retrieved from ArcSDE.


Code:
SeRow row = query.fetch();
Date dateObj = row.getDate(columnPos);
Calendar gmt = Calendar.getInstance();
gmt.setTimeZone( TimeZone.getTimeZone("GMT") );
gmt.setTime(dateObj);

Calendar local = Calendar.getInstance();
int sec = gmt.get(Calendar.SECOND);
int min = gmt.get(Calendar.MINUTE);
int hour = gmt.get(Calendar.HOUR_OF_DAY);
int date = gmt.get(Calendar.DATE);
int month = gmt.get(Calendar.MONTH);
int year = gmt.get(Calendar.YEAR);
local.set(year, month, date, hour, min, sec);

System.out.println( "Local time " + local.getTime());