datetime.java
来自「derby database source code.good for you.」· Java 代码 · 共 781 行 · 第 1/3 页
JAVA
781 行
return DateTime.dateRepresentationLength; } /** * java.sql.Time is converted to character representation which is in JDBC time escape * format: <code>hh:mm:ss</code>, which is the same as JIS time format in DERBY string * representation of a time. The char representation is converted to bytes using UTF8 * encoding. * @param buffer bytes in UTF8 encoding of the time * @param offset write into the buffer from this offset * @param time java.sql.Time value * @return DateTime.timeRepresentationLength. This is the fixed length in * bytes taken to represent the time value * @throws UnsupportedEncodingException */ public static final int timeToTimeBytes(byte[] buffer, int offset, java.sql.Time time) throws UnsupportedEncodingException { int hour = time.getHours(); int minute = time.getMinutes(); int second = time.getSeconds(); char[] timeChars = new char[DateTime.timeRepresentationLength]; int zeroBase = (int) '0'; timeChars[0] = (char) (hour / 10 + zeroBase); timeChars[1] = (char) (hour % 10 + +zeroBase); timeChars[2] = ':'; timeChars[3] = (char) (minute / 10 + zeroBase); timeChars[4] = (char) (minute % 10 + zeroBase); timeChars[5] = ':'; timeChars[6] = (char) (second / 10 + zeroBase); timeChars[7] = (char) (second % 10 + zeroBase); // Network server expects to read the time parameter value bytes with // UTF-8 encoding. Reference - DERBY-1127 // see DRDAConnThread.readAndSetParams byte[] timeBytes = (new String(timeChars)).getBytes(Typdef.UTF8ENCODING); System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); return DateTime.timeRepresentationLength; } /** * java.sql.Timestamp is converted to a character representation which is in DERBY string * representation of a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. * and then converted to bytes using UTF8 encoding * @param buffer bytes in UTF8 encoding of the timestamp * @param offset write into the buffer from this offset * @param timestamp timestamp value * @return DateTime.timestampRepresentationLength. This is the fixed * length in bytes, taken to represent the timestamp value * @throws SqlException * @throws UnsupportedEncodingException */ public static final int timestampToTimestampBytes(byte[] buffer, int offset, java.sql.Timestamp timestamp) throws ConversionException,UnsupportedEncodingException { int year = timestamp.getYear() + 1900; if (year > 9999) { throw new ConversionException("Year exceeds the maximum \"9999\"."); } int month = timestamp.getMonth() + 1; int day = timestamp.getDate(); int hour = timestamp.getHours(); int minute = timestamp.getMinutes(); int second = timestamp.getSeconds(); int microsecond = timestamp.getNanos() / 1000; char[] timestampChars = new char[DateTime.timestampRepresentationLength]; int zeroBase = (int) '0'; timestampChars[0] = (char) (year / 1000 + zeroBase); timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase); timestampChars[2] = (char) ((year % 100) / 10 + zeroBase); timestampChars[3] = (char) (year % 10 + +zeroBase); timestampChars[4] = '-'; timestampChars[5] = (char) (month / 10 + zeroBase); timestampChars[6] = (char) (month % 10 + zeroBase); timestampChars[7] = '-'; timestampChars[8] = (char) (day / 10 + zeroBase); timestampChars[9] = (char) (day % 10 + zeroBase); timestampChars[10] = '-'; timestampChars[11] = (char) (hour / 10 + zeroBase); timestampChars[12] = (char) (hour % 10 + zeroBase); timestampChars[13] = '.'; timestampChars[14] = (char) (minute / 10 + zeroBase); timestampChars[15] = (char) (minute % 10 + zeroBase); timestampChars[16] = '.'; timestampChars[17] = (char) (second / 10 + zeroBase); timestampChars[18] = (char) (second % 10 + zeroBase); timestampChars[19] = '.'; timestampChars[20] = (char) (microsecond / 100000 + zeroBase); timestampChars[21] = (char) ((microsecond % 100000) / 10000 + zeroBase); timestampChars[22] = (char) ((microsecond % 10000) / 1000 + zeroBase); timestampChars[23] = (char) ((microsecond % 1000) / 100 + zeroBase); timestampChars[24] = (char) ((microsecond % 100) / 10 + zeroBase); timestampChars[25] = (char) (microsecond % 10 + zeroBase); // Network server expects to read the timestamp parameter value bytes with // UTF-8 encoding. Reference - DERBY-1127 // see DRDAConnThread.readAndSetParams byte[] timestampBytes = (new String(timestampChars)).getBytes(Typdef.UTF8ENCODING); System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); return DateTime.timestampRepresentationLength; } // ********************************************************* // ******* CROSS output converters (byte[] -> class) ******* // ********************************************************* /** * Expected character representation is DERBY string representation of a date * which is in JIS format: <code> yyyy-mm-dd </code> * * @param buffer * @param offset * @param recyclableTimestamp * @param encoding encoding of buffer * @return * @throws UnsupportedEncodingException */ public static final java.sql.Timestamp dateBytesToTimestamp(byte[] buffer, int offset, java.sql.Timestamp recyclableTimestamp, String encoding) throws UnsupportedEncodingException { int year, month, day; String date = new String(buffer, offset, DateTime.dateRepresentationLength, encoding); int yearIndx, monthIndx, dayIndx; yearIndx = 0; monthIndx = 5; dayIndx = 8; int zeroBase = ((int) '0'); // Character arithmetic is used rather than // the less efficient Integer.parseInt (date.substring()). year = 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) + 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) + (((int) date.charAt(yearIndx + 3)) - zeroBase) - 1900; month = 10 * (((int) date.charAt(monthIndx)) - zeroBase) + (((int) date.charAt(monthIndx + 1)) - zeroBase) - 1; day = 10 * (((int) date.charAt(dayIndx)) - zeroBase) + (((int) date.charAt(dayIndx + 1)) - zeroBase); if (recyclableTimestamp == null) { return new java.sql.Timestamp(year, month, day, 0, 0, 0, 0); } else { recyclableTimestamp.setYear(year); recyclableTimestamp.setMonth(month); recyclableTimestamp.setDate(day); recyclableTimestamp.setHours(0); recyclableTimestamp.setMinutes(0); recyclableTimestamp.setSeconds(0); recyclableTimestamp.setNanos(0); return recyclableTimestamp; } } /** * Expected character representation is DERBY string representation of time * which is in the format: <code> hh.mm.ss </code> * * @param buffer * @param offset * @param recyclableTimestamp * @param encoding encoding of buffer * @return * @throws UnsupportedEncodingException * */ public static final java.sql.Timestamp timeBytesToTimestamp(byte[] buffer, int offset, java.sql.Timestamp recyclableTimestamp, String encoding) throws UnsupportedEncodingException { int hour, minute, second; String time = new String(buffer, offset, DateTime.timeRepresentationLength, encoding); int zeroBase = ((int) '0'); // compute hour. hour = 10 * (((int) time.charAt(0)) - zeroBase) + (((int) time.charAt(1)) - zeroBase); // compute minute. minute = 10 * (((int) time.charAt(3)) - zeroBase) + (((int) time.charAt(4)) - zeroBase); // compute second JIS format: hh:mm:ss. second = 10 * (((int) time.charAt(6)) - zeroBase) + (((int) time.charAt(7)) - zeroBase); if (recyclableTimestamp == null) { return new java.sql.Timestamp(0, 0, 1, hour, minute, second, 0); } else { recyclableTimestamp.setYear(0); recyclableTimestamp.setMonth(0); recyclableTimestamp.setDate(1); recyclableTimestamp.setHours(hour); recyclableTimestamp.setMinutes(minute); recyclableTimestamp.setSeconds(second); recyclableTimestamp.setNanos(0); return recyclableTimestamp; } } /** * Expected character representation is DERBY string representation of a timestamp: * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. * * @param buffer * @param offset * @param recyclableDate * @param encoding encoding of buffer * @return * @throws UnsupportedEncodingException */ public static final java.sql.Date timestampBytesToDate(byte[] buffer, int offset, java.sql.Date recyclableDate, String encoding) throws UnsupportedEncodingException { int year, month, day; String timestamp = new String(buffer, offset, DateTime.timestampRepresentationLength, encoding); int zeroBase = ((int) '0'); year = 1000 * (((int) timestamp.charAt(0)) - zeroBase) + 100 * (((int) timestamp.charAt(1)) - zeroBase) + 10 * (((int) timestamp.charAt(2)) - zeroBase) + (((int) timestamp.charAt(3)) - zeroBase) - 1900; month = 10 * (((int) timestamp.charAt(5)) - zeroBase) + (((int) timestamp.charAt(6)) - zeroBase) - 1; day = 10 * (((int) timestamp.charAt(8)) - zeroBase) + (((int) timestamp.charAt(9)) - zeroBase); if (recyclableDate == null) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?