Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

setTimestamp problem for chinese timezone

Help
2012-10-11
2014-01-19
  • Stefan Heller
    Stefan Heller
    2012-10-11

    I have a HSQLDB  2.2.8 running under the following timezone:

    (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi

    and the following locale:

    en-us;English (United States)

    Sometimes the following problem occurs by calling the method setTimestamp() on a prepared statement:

    java.lang.IllegalArgumentException: YEAR

    java.util.GregorianCalendar.computeTime(Unknown Source)
    java.util.Calendar.updateTime(Unknown Source)
    java.util.Calendar.getTimeInMillis(Unknown Source)
    org.hsqldb.HsqlDateTime.convertMillisFromCalendar(Unknown Source)
    org.hsqldb.types.DateTimeType.convertJavaToSQL(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.setTimestamp(Unknown Source)

    I have no further information about the real date that creates this problem - all I can say is that this seems to be seldom and I cannot find a special date when this happens.

    Is that a problem that could be fixed by other timezone conversion settings on the database (SET DATABASE SQL TRANSLATE TTI TYPES { TRUE | FALSE }), is this a problem of the jdbc-java date conversion or is it a better approach to store the dates using their milliseconds in a "long" field on the database to prevent the date conversion and let Java handle the time zones?

    Thanks
    Heller

     
  • Fred Toussi
    Fred Toussi
    2012-10-11

    The Calendar used in the stack trace is set to the session time zone. It is theoretically possible that some UTC timestamps do not correspond to a valid date in a given time zone. This might happen when daylight saving time is changed.

    Therefore, the Java Timesamp that you use must be valid for your session's Calendar. You may be using a timesamp that is invalid.

    If you cannot guarantee your Timesamp is valid, you should set the session time zone as UTC. All Java Timestamps are valid in UTC. Alternatively, use the other setTimestamp() method with a UTC Calendar.

     
  • Stefan Heller
    Stefan Heller
    2012-10-11

    Thank you for this fast answer, I will try this!

     
  • Fred Toussi
    Fred Toussi
    2012-10-11

    It would also be a good idea to log the Timestamp millisecond value when the error occurs so that you can check it later.