Draft 01/30/01

com.ibm.converters
Class DateTime

java.lang.Object
  |
  +--com.ibm.converters.DateTime

public class DateTime
extends Object

High performance converters from date/time byte encodings to JDBC Date, Time and Timestamp objects.

Using this class for direct date/time conversions from bytes offers superior performance over the alternative method of first constructing a Java String from the encoded bytes, and then using java.sql.Date.valueOf(), java.sql.Time.valueOf() or java.sql.Timestamp.valueOf().

These DateTime converters also allow for a variation of the ISO date/time representations used by DB2 which is not supported by java.sql.Date.valueOf(), java.sql.Time.valueOf() or java.sql.Timestamp.valueOf(). For example, both hh.mm.ss or the ISO representation hh:mm:ss is supported for time bytes.

Common encodings are special cased to bypass any String constructor calls altogether. For the uncommon encodings, Unicode character arithmetic is used rather than the less efficient Integer.parseInt (date.substring()) used by java.sql.Date.valueOf(), java.sql.Time.valueOf() and java.sql.Timestamp.valueOf().

For EBCDIC the Java encoding name is Cp500. For a complete list of supported encodings and their CCSID equivalents see CCSID To Java Encoding Mapping.

Author:
Paul Ostler, John Vonau, Qin Liu, reviewed by Mike Fulton

Method Summary
static Date dateBytesToDate(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is of the form yyyy-mm-dd.
static Timestamp dateBytesToTimestamp(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is of the form yyyy-mm-dd.
static Time timeBytesToTime(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is either of the form hh.mm.ss or the ISO representation hh:mm:ss.
static Timestamp timeBytesToTimestamp(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is either of the form hh.mm.ss or the ISO representation hh:mm:ss.
static Date timestampBytesToDate(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is of the form yyyy-mm-dd-hh.mm.ss.ffffff or the ISO representation yyyy-mm-dd hh:mm:ss.ffffff.
static Time timestampBytesToTime(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is of the form yyyy-mm-dd-hh.mm.ss.ffffff or the ISO representation yyyy-mm-dd hh:mm:ss.ffffff.
static Timestamp timestampBytesToTimestamp(byte[] buffer, int offset, int length, String encoding)
          Expected character representation is of the form yyyy-mm-dd-hh.mm.ss.ffffff or the ISO representation yyyy-mm-dd hh:mm:ss.ffffff.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

dateBytesToDate

public static Date dateBytesToDate(byte[] buffer,
                                   int offset,
                                   int length,
                                   String encoding)
                            throws UnsupportedEncodingException
Expected character representation is of the form yyyy-mm-dd. So for a single-byte character encoding, the length should be 10.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

dateBytesToTimestamp

public static Timestamp dateBytesToTimestamp(byte[] buffer,
                                             int offset,
                                             int length,
                                             String encoding)
                                      throws UnsupportedEncodingException
Expected character representation is of the form yyyy-mm-dd. So for a single-byte character encoding, the length should be 10.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

timeBytesToTime

public static Time timeBytesToTime(byte[] buffer,
                                   int offset,
                                   int length,
                                   String encoding)
                            throws UnsupportedEncodingException
Expected character representation is either of the form hh.mm.ss or the ISO representation hh:mm:ss. So for a single-byte character encoding, the length should be 8.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

timeBytesToTimestamp

public static Timestamp timeBytesToTimestamp(byte[] buffer,
                                             int offset,
                                             int length,
                                             String encoding)
                                      throws UnsupportedEncodingException
Expected character representation is either of the form hh.mm.ss or the ISO representation hh:mm:ss. So for a single-byte character encoding, the length should be 8.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

timestampBytesToTimestamp

public static Timestamp timestampBytesToTimestamp(byte[] buffer,
                                                  int offset,
                                                  int length,
                                                  String encoding)
                                           throws UnsupportedEncodingException
Expected character representation is of the form yyyy-mm-dd-hh.mm.ss.ffffff or the ISO representation yyyy-mm-dd hh:mm:ss.ffffff. So for a single-byte character encoding, the length should be 26.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

timestampBytesToDate

public static Date timestampBytesToDate(byte[] buffer,
                                        int offset,
                                        int length,
                                        String encoding)
                                 throws UnsupportedEncodingException
Expected character representation is of the form yyyy-mm-dd-hh.mm.ss.ffffff or the ISO representation yyyy-mm-dd hh:mm:ss.ffffff. So for a single-byte character encoding, the length should be 26.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

timestampBytesToTime

public static Time timestampBytesToTime(byte[] buffer,
                                        int offset,
                                        int length,
                                        String encoding)
                                 throws UnsupportedEncodingException
Expected character representation is of the form yyyy-mm-dd-hh.mm.ss.ffffff or the ISO representation yyyy-mm-dd hh:mm:ss.ffffff. So for a single-byte character encoding, the length should be 26.
Throws:
UnsupportedEncodingException - if the provided character encoding is unsupported.

Draft 01/30/01

Send comments or suggestions to jcc@us.ibm.com