📄 datetime.java
字号:
/* Derby - Class org.apache.derby.client.am.DateTime Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/package org.apache.derby.client.am;import java.io.UnsupportedEncodingException;import org.apache.derby.client.net.Typdef;/** * High performance converters from date/time byte encodings to JDBC Date, Time and Timestamp objects. * <p/> * 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 {@link java.sql.Date#valueOf * java.sql.Date.valueOf()}, {@link java.sql.Time#valueOf java.sql.Time.valueOf()} or {@link java.sql.Timestamp#valueOf * java.sql.Timestamp.valueOf()}. * <p/> */public class DateTime { // Hide the default constructor private DateTime() { } private static final int dateRepresentationLength = 10; private static final int timeRepresentationLength = 8; private static final int timestampRepresentationLength = 26; // ********************************************************* // ********** 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 recyclableDate * @param encoding encoding of buffer data * @return * @throws UnsupportedEncodingException */ public static final java.sql.Date dateBytesToDate(byte[] buffer, int offset, java.sql.Date recyclableDate, String encoding) throws UnsupportedEncodingException { int year, month, day; String date = new String(buffer, offset, DateTime.dateRepresentationLength,encoding); int yearIndx, monthIndx, dayIndx; if (date.charAt(4) == '-') { // JIS format: yyyy-mm-dd. yearIndx = 0; monthIndx = 5; dayIndx = 8; } else { throw new java.lang.IllegalArgumentException("Unsupported date format!"); } 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 (recyclableDate == null) { return new java.sql.Date(year, month, day); } else { recyclableDate.setYear(year); recyclableDate.setMonth(month); recyclableDate.setDate(day); return recyclableDate; } } /** * Expected character representation is DERBY string representation of time, * which is in the format: <code> hh.mm.ss </code> * @param buffer * @param offset * @param recyclableTime * @param encoding encoding of buffer * @return * @throws UnsupportedEncodingException */ public static final java.sql.Time timeBytesToTime(byte[] buffer, int offset, java.sql.Time recyclableTime, 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. second = 10 * (((int) time.charAt(6)) - zeroBase) + (((int) time.charAt(7)) - zeroBase); if (recyclableTime == null) { return new java.sql.Time(hour, minute, second); } else { recyclableTime.setHours(hour); recyclableTime.setMinutes(minute); recyclableTime.setSeconds(second); return recyclableTime; } } /** * Expected character representation is DERBY string representation of a timestamp: * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. * * @param buffer * @param offset * @param recyclableTimestamp * @param encoding encoding of buffer * @return * @throws UnsupportedEncodingException */ public static final java.sql.Timestamp timestampBytesToTimestamp(byte[] buffer, int offset, java.sql.Timestamp recyclableTimestamp, String encoding) throws UnsupportedEncodingException { int year, month, day, hour, minute, second, fraction; 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); hour = 10 * (((int) timestamp.charAt(11)) - zeroBase) + (((int) timestamp.charAt(12)) - zeroBase); minute = 10 * (((int) timestamp.charAt(14)) - zeroBase) + (((int) timestamp.charAt(15)) - zeroBase); second = 10 * (((int) timestamp.charAt(17)) - zeroBase) + (((int) timestamp.charAt(18)) - zeroBase); fraction = 100000 * (((int) timestamp.charAt(20)) - zeroBase) + 10000 * (((int) timestamp.charAt(21)) - zeroBase) + 1000 * (((int) timestamp.charAt(22)) - zeroBase) + 100 * (((int) timestamp.charAt(23)) - zeroBase) + 10 * (((int) timestamp.charAt(24)) - zeroBase) + (((int) timestamp.charAt(25)) - zeroBase); if (recyclableTimestamp == null) { return new java.sql.Timestamp(year, month, day, hour, minute, second, fraction * 1000); } else { recyclableTimestamp.setYear(year); recyclableTimestamp.setMonth(month); recyclableTimestamp.setDate(day); recyclableTimestamp.setHours(hour); recyclableTimestamp.setMinutes(minute); recyclableTimestamp.setSeconds(second); recyclableTimestamp.setNanos(fraction * 1000); return recyclableTimestamp; } } // ******************************************************** // ********** Input converters (class -> byte[]) ********** // ******************************************************** /** * Date is converted to a char representation in JDBC date format: <code>yyyy-mm-dd</code> date format * and then converted to bytes using UTF8 encoding * @param buffer bytes in UTF8 encoding of the date * @param offset write into the buffer from this offset * @param date date value * @return DateTime.dateRepresentationLength. This is the fixed length in * bytes taken to represent the date value * @throws SqlException * @throws UnsupportedEncodingException if UTF8 Encoding is not supported */ public static final int dateToDateBytes(byte[] buffer, int offset, java.sql.Date date) throws ConversionException,UnsupportedEncodingException { int year = date.getYear() + 1900; if (year > 9999) { throw new ConversionException("Year exceeds the maximum \"9999\"."); } int month = date.getMonth() + 1; int day = date.getDate(); char[] dateChars = new char[DateTime.dateRepresentationLength]; int zeroBase = (int) '0'; dateChars[0] = (char) (year / 1000 + zeroBase); dateChars[1] = (char) ((year % 1000) / 100 + zeroBase); dateChars[2] = (char) ((year % 100) / 10 + zeroBase); dateChars[3] = (char) (year % 10 + +zeroBase); dateChars[4] = '-'; dateChars[5] = (char) (month / 10 + zeroBase); dateChars[6] = (char) (month % 10 + zeroBase); dateChars[7] = '-'; dateChars[8] = (char) (day / 10 + zeroBase); dateChars[9] = (char) (day % 10 + zeroBase); // Network server expects to read the date parameter value bytes with // UTF-8 encoding. Reference - DERBY-1127 // see DRDAConnThread.readAndSetParams byte[] dateBytes = (new String(dateChars)).getBytes(Typdef.UTF8ENCODING); System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -