📄 datetime.java
字号:
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 a timestamp: * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>. * * @param buffer * @param offset * @param recyclableTime * @param encoding encoding of buffer * @return * @throws UnsupportedEncodingException */ public static final java.sql.Time timestampBytesToTime(byte[] buffer, int offset, java.sql.Time recyclableTime, String encoding) throws UnsupportedEncodingException { int hour, minute, second; String timestamp = new String(buffer, offset, DateTime.timestampRepresentationLength, encoding); int zeroBase = ((int) '0'); 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); if (recyclableTime == null) { return new java.sql.Time(hour, minute, second); } else { recyclableTime.setYear(hour); recyclableTime.setMonth(minute); recyclableTime.setDate(second); return recyclableTime; } } // ********************************************************* // ******* CROSS input converters (class -> byte[]) ******** // ********************************************************* /** * java.sql.Timestamp is converted to character representation that is in JDBC date escape * format: <code>yyyy-mm-dd</code>, which is the same as JIS date format in DERBY string representation of a date. * and then converted to bytes using UTF8 encoding. * @param buffer * @param offset write into the buffer from this offset * @param timestamp timestamp value * @return DateTime.dateRepresentationLength. This is the fixed length * in bytes, that is taken to represent the timestamp value as a date. * @throws ConversionException * @throws UnsupportedEncodingException */ public static final int timestampToDateBytes(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(); 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); return DateTime.dateRepresentationLength; } /** * java.sql.Timestamp is converted to character representation 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 * then converted to bytes using UTF8 encoding and written out into the buffer * @param buffer * @param offset write into the buffer from this offset * @param timestamp timestamp value * @return DateTime.timeRepresentationLength. This is the fixed length * in bytes taken to represent the timestamp value as Time. * @throws UnsupportedEncodingException */ public static final int timestampToTimeBytes(byte[] buffer, int offset, java.sql.Timestamp timestamp) throws UnsupportedEncodingException { int hour = timestamp.getHours(); int minute = timestamp.getMinutes(); int second = timestamp.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.Date is converted to character representation that 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 and written out to the buffer * @param buffer * @param offset offset in buffer to start writing to * @param date date value * @return DateTime.timestampRepresentationLength. This is the fixed length * in bytes, taken to represent the timestamp value. * @throws ConversionException * @throws UnsupportedEncodingException */ public static final int dateToTimestampBytes(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[] 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] = '0'; timestampChars[12] = '0'; timestampChars[13] = '.'; timestampChars[14] = '0'; timestampChars[15] = '0'; timestampChars[16] = '.'; timestampChars[17] = '0'; timestampChars[18] = '0'; timestampChars[19] = '.'; timestampChars[20] = '0'; timestampChars[21] = '0'; timestampChars[22] = '0'; timestampChars[23] = '0'; timestampChars[24] = '0'; timestampChars[25] = '0'; // 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; } /** * java.sql.Time is converted to a character representation that is in DERBY string representation of a timestamp: * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code> and converted to bytes using UTF8 encoding * @param buffer * @param offset offset in buffer to start writing to * @param time time value * @return DateTime.timestampRepresentationLength which is the fixed length * taken up by the conversion of time to timestamp in bytes * @throws UnsupportedEncodingException */ public static final int timeToTimestampBytes(byte[] buffer, int offset, java.sql.Time time) throws UnsupportedEncodingException { int hour = time.getHours(); int minute = time.getMinutes(); int second = time.getSeconds(); char[] timestampChars = new char[DateTime.timestampRepresentationLength]; int zeroBase = (int) '0'; timestampChars[0] = '1'; timestampChars[1] = '9'; timestampChars[2] = '0'; timestampChars[3] = '0'; timestampChars[4] = '-'; timestampChars[5] = '0'; timestampChars[6] = '1'; timestampChars[7] = '-'; timestampChars[8] = '0'; timestampChars[9] = '1'; 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] = '0'; timestampChars[21] = '0'; timestampChars[22] = '0'; timestampChars[23] = '0'; timestampChars[24] = '0'; timestampChars[25] = '0'; // Network server expects to read the timestamp parameter value bytes with // UTF-8 encoding. Reference - DERBY-1127 // see DRDAConnThread.readAndSetParams for TIMESTAMP byte[] timestampBytes = (new String(timestampChars)).getBytes(Typdef.UTF8ENCODING); System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); return DateTime.timestampRepresentationLength; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -