Source Code Cross Referenced for DateTime.java in  » Database-DBMS » db-derby-10.2 » org » apache » derby » client » am » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derby.client.am 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derby.client.am.DateTime
004:
005:           Licensed to the Apache Software Foundation (ASF) under one or more
006:           contributor license agreements.  See the NOTICE file distributed with
007:           this work for additional information regarding copyright ownership.
008:           The ASF licenses this file to You under the Apache License, Version 2.0
009:           (the "License"); you may not use this file except in compliance with
010:           the License.  You may obtain a copy of the License at
011:
012:              http://www.apache.org/licenses/LICENSE-2.0
013:
014:           Unless required by applicable law or agreed to in writing, software
015:           distributed under the License is distributed on an "AS IS" BASIS,
016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:           See the License for the specific language governing permissions and
018:           limitations under the License.
019:
020:         */
021:        package org.apache.derby.client.am;
022:
023:        import org.apache.derby.shared.common.i18n.MessageUtil;
024:        import org.apache.derby.shared.common.reference.SQLState;
025:
026:        import java.io.UnsupportedEncodingException;
027:        import org.apache.derby.client.net.Typdef;
028:
029:        /**
030:         * High performance converters from date/time byte encodings to JDBC Date, Time and Timestamp objects.
031:         * <p/>
032:         * Using this class for direct date/time conversions from bytes offers superior performance over the alternative method
033:         * of first constructing a Java String from the encoded bytes, and then using {@link java.sql.Date#valueOf
034:         * java.sql.Date.valueOf()}, {@link java.sql.Time#valueOf java.sql.Time.valueOf()} or {@link java.sql.Timestamp#valueOf
035:         * java.sql.Timestamp.valueOf()}.
036:         * <p/>
037:         */
038:        public class DateTime {
039:
040:            // Hide the default constructor
041:            private DateTime() {
042:            }
043:
044:            private static final int dateRepresentationLength = 10;
045:            private static final int timeRepresentationLength = 8;
046:            private static final int timestampRepresentationLength = 26;
047:
048:            // *********************************************************
049:            // ********** Output converters (byte[] -> class) **********
050:            // *********************************************************
051:
052:            /**
053:             * Expected character representation is DERBY string representation of a date, 
054:             * which is in JIS format: <code> yyyy-mm-dd </code>
055:             * 
056:             * @param buffer    
057:             * @param offset    
058:             * @param recyclableDate
059:             * @param encoding            encoding of buffer data
060:             * @return  Date translated from  buffer with specified encoding
061:             * @throws UnsupportedEncodingException
062:             */
063:            public static final java.sql.Date dateBytesToDate(byte[] buffer,
064:                    int offset, java.sql.Date recyclableDate, String encoding)
065:                    throws UnsupportedEncodingException {
066:                int year, month, day;
067:
068:                String date = new String(buffer, offset,
069:                        DateTime.dateRepresentationLength, encoding);
070:                int yearIndx, monthIndx, dayIndx;
071:                if (date.charAt(4) == '-') {
072:                    // JIS format: yyyy-mm-dd.
073:                    yearIndx = 0;
074:                    monthIndx = 5;
075:                    dayIndx = 8;
076:                } else {
077:                    throw new java.lang.IllegalArgumentException(SqlException
078:                            .getMessageUtil().getTextMessage(
079:                                    SQLState.LANG_FORMAT_EXCEPTION));
080:                }
081:
082:                int zeroBase = ((int) '0');
083:                // Character arithmetic is used rather than
084:                // the less efficient Integer.parseInt (date.substring()).
085:                year = 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + 100
086:                        * (((int) date.charAt(yearIndx + 1)) - zeroBase) + 10
087:                        * (((int) date.charAt(yearIndx + 2)) - zeroBase)
088:                        + (((int) date.charAt(yearIndx + 3)) - zeroBase) - 1900;
089:                month = 10 * (((int) date.charAt(monthIndx)) - zeroBase)
090:                        + (((int) date.charAt(monthIndx + 1)) - zeroBase) - 1;
091:                day = 10 * (((int) date.charAt(dayIndx)) - zeroBase)
092:                        + (((int) date.charAt(dayIndx + 1)) - zeroBase);
093:
094:                if (recyclableDate == null) {
095:                    return new java.sql.Date(year, month, day);
096:                } else {
097:                    recyclableDate.setYear(year);
098:                    recyclableDate.setMonth(month);
099:                    recyclableDate.setDate(day);
100:                    return recyclableDate;
101:                }
102:            }
103:
104:            /**
105:             * Expected character representation is DERBY string representation of time,
106:             * which is in the format: <code> hh.mm.ss </code>
107:             * @param buffer
108:             * @param offset
109:             * @param recyclableTime
110:             * @param encoding           encoding of buffer
111:             * @return  Time translated from buffer with specified encoding
112:             * @throws UnsupportedEncodingException
113:             */
114:            public static final java.sql.Time timeBytesToTime(byte[] buffer,
115:                    int offset, java.sql.Time recyclableTime, String encoding)
116:                    throws UnsupportedEncodingException {
117:                int hour, minute, second;
118:
119:                String time = new String(buffer, offset,
120:                        DateTime.timeRepresentationLength, encoding);
121:                int zeroBase = ((int) '0');
122:
123:                // compute hour.
124:                hour = 10 * (((int) time.charAt(0)) - zeroBase)
125:                        + (((int) time.charAt(1)) - zeroBase);
126:                // compute minute.
127:                minute = 10 * (((int) time.charAt(3)) - zeroBase)
128:                        + (((int) time.charAt(4)) - zeroBase);
129:                // compute second.
130:                second = 10 * (((int) time.charAt(6)) - zeroBase)
131:                        + (((int) time.charAt(7)) - zeroBase);
132:
133:                if (recyclableTime == null) {
134:                    return new java.sql.Time(hour, minute, second);
135:                } else {
136:                    recyclableTime.setHours(hour);
137:                    recyclableTime.setMinutes(minute);
138:                    recyclableTime.setSeconds(second);
139:                    return recyclableTime;
140:                }
141:            }
142:
143:            /**
144:             * Expected character representation is DERBY string representation of a timestamp:
145:             * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
146:             * 
147:             * @param buffer
148:             * @param offset
149:             * @param recyclableTimestamp
150:             * @param encoding                encoding of buffer
151:             * @return TimeStamp translated from buffer with specified encoding
152:             * @throws UnsupportedEncodingException
153:             */
154:            public static final java.sql.Timestamp timestampBytesToTimestamp(
155:                    byte[] buffer, int offset,
156:                    java.sql.Timestamp recyclableTimestamp, String encoding)
157:                    throws UnsupportedEncodingException {
158:                int year, month, day, hour, minute, second, fraction;
159:                String timestamp = new String(buffer, offset,
160:                        DateTime.timestampRepresentationLength, encoding);
161:
162:                int zeroBase = ((int) '0');
163:
164:                year = 1000 * (((int) timestamp.charAt(0)) - zeroBase) + 100
165:                        * (((int) timestamp.charAt(1)) - zeroBase) + 10
166:                        * (((int) timestamp.charAt(2)) - zeroBase)
167:                        + (((int) timestamp.charAt(3)) - zeroBase) - 1900;
168:                month = 10 * (((int) timestamp.charAt(5)) - zeroBase)
169:                        + (((int) timestamp.charAt(6)) - zeroBase) - 1;
170:                day = 10 * (((int) timestamp.charAt(8)) - zeroBase)
171:                        + (((int) timestamp.charAt(9)) - zeroBase);
172:                hour = 10 * (((int) timestamp.charAt(11)) - zeroBase)
173:                        + (((int) timestamp.charAt(12)) - zeroBase);
174:                minute = 10 * (((int) timestamp.charAt(14)) - zeroBase)
175:                        + (((int) timestamp.charAt(15)) - zeroBase);
176:                second = 10 * (((int) timestamp.charAt(17)) - zeroBase)
177:                        + (((int) timestamp.charAt(18)) - zeroBase);
178:                fraction = 100000 * (((int) timestamp.charAt(20)) - zeroBase)
179:                        + 10000 * (((int) timestamp.charAt(21)) - zeroBase)
180:                        + 1000 * (((int) timestamp.charAt(22)) - zeroBase)
181:                        + 100 * (((int) timestamp.charAt(23)) - zeroBase) + 10
182:                        * (((int) timestamp.charAt(24)) - zeroBase)
183:                        + (((int) timestamp.charAt(25)) - zeroBase);
184:
185:                if (recyclableTimestamp == null) {
186:                    return new java.sql.Timestamp(year, month, day, hour,
187:                            minute, second, fraction * 1000);
188:                } else {
189:                    recyclableTimestamp.setYear(year);
190:                    recyclableTimestamp.setMonth(month);
191:                    recyclableTimestamp.setDate(day);
192:                    recyclableTimestamp.setHours(hour);
193:                    recyclableTimestamp.setMinutes(minute);
194:                    recyclableTimestamp.setSeconds(second);
195:                    recyclableTimestamp.setNanos(fraction * 1000);
196:                    return recyclableTimestamp;
197:                }
198:            }
199:
200:            // ********************************************************
201:            // ********** Input converters (class -> byte[]) **********
202:            // ********************************************************
203:
204:            /**
205:             * Date is converted to a char representation in JDBC date format: <code>yyyy-mm-dd</code> date format
206:             * and then converted to bytes using UTF8 encoding
207:             * @param buffer  bytes in UTF8 encoding of the date
208:             * @param offset  write into the buffer from this offset 
209:             * @param date    date value
210:             * @return DateTime.dateRepresentationLength. This is the fixed length in 
211:             * bytes taken to represent the date value
212:             * @throws SqlException
213:             * @throws UnsupportedEncodingException if UTF8 Encoding is not supported
214:             */
215:            public static final int dateToDateBytes(byte[] buffer, int offset,
216:                    java.sql.Date date) throws SqlException,
217:                    UnsupportedEncodingException {
218:                int year = date.getYear() + 1900;
219:                if (year > 9999) {
220:                    throw new SqlException(null, new ClientMessageId(
221:                            SQLState.YEAR_EXCEEDS_MAXIMUM), new Integer(year),
222:                            "9999");
223:                }
224:                int month = date.getMonth() + 1;
225:                int day = date.getDate();
226:
227:                char[] dateChars = new char[DateTime.dateRepresentationLength];
228:                int zeroBase = (int) '0';
229:                dateChars[0] = (char) (year / 1000 + zeroBase);
230:                dateChars[1] = (char) ((year % 1000) / 100 + zeroBase);
231:                dateChars[2] = (char) ((year % 100) / 10 + zeroBase);
232:                dateChars[3] = (char) (year % 10 + +zeroBase);
233:                dateChars[4] = '-';
234:                dateChars[5] = (char) (month / 10 + zeroBase);
235:                dateChars[6] = (char) (month % 10 + zeroBase);
236:                dateChars[7] = '-';
237:                dateChars[8] = (char) (day / 10 + zeroBase);
238:                dateChars[9] = (char) (day % 10 + zeroBase);
239:
240:                // Network server expects to read the date parameter value bytes with
241:                // UTF-8 encoding.  Reference - DERBY-1127
242:                // see DRDAConnThread.readAndSetParams
243:                byte[] dateBytes = (new String(dateChars))
244:                        .getBytes(Typdef.UTF8ENCODING);
245:                System.arraycopy(dateBytes, 0, buffer, offset,
246:                        DateTime.dateRepresentationLength);
247:
248:                return DateTime.dateRepresentationLength;
249:            }
250:
251:            /**
252:             * java.sql.Time is converted to character representation which is in JDBC time escape
253:             * format: <code>hh:mm:ss</code>, which is the same as JIS time format in DERBY string 
254:             * representation of a time.  The char representation is converted to bytes using UTF8 
255:             * encoding.
256:             * @param buffer  bytes in UTF8 encoding of the time
257:             * @param offset  write into the buffer from this offset 
258:             * @param time  java.sql.Time value
259:             * @return DateTime.timeRepresentationLength. This is the fixed length in 
260:             * bytes taken to represent the time value
261:             * @throws UnsupportedEncodingException
262:             */
263:            public static final int timeToTimeBytes(byte[] buffer, int offset,
264:                    java.sql.Time time) throws UnsupportedEncodingException {
265:                int hour = time.getHours();
266:                int minute = time.getMinutes();
267:                int second = time.getSeconds();
268:
269:                char[] timeChars = new char[DateTime.timeRepresentationLength];
270:                int zeroBase = (int) '0';
271:                timeChars[0] = (char) (hour / 10 + zeroBase);
272:                timeChars[1] = (char) (hour % 10 + +zeroBase);
273:                timeChars[2] = ':';
274:                timeChars[3] = (char) (minute / 10 + zeroBase);
275:                timeChars[4] = (char) (minute % 10 + zeroBase);
276:                timeChars[5] = ':';
277:                timeChars[6] = (char) (second / 10 + zeroBase);
278:                timeChars[7] = (char) (second % 10 + zeroBase);
279:
280:                // Network server expects to read the time parameter value bytes with
281:                // UTF-8 encoding.  Reference - DERBY-1127
282:                // see DRDAConnThread.readAndSetParams
283:                byte[] timeBytes = (new String(timeChars))
284:                        .getBytes(Typdef.UTF8ENCODING);
285:                System.arraycopy(timeBytes, 0, buffer, offset,
286:                        DateTime.timeRepresentationLength);
287:
288:                return DateTime.timeRepresentationLength;
289:            }
290:
291:            /**
292:             * java.sql.Timestamp is converted to a character representation which is in DERBY string 
293:             * representation of a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
294:             * and then converted to bytes using UTF8 encoding
295:             * @param buffer  bytes in UTF8 encoding of the timestamp
296:             * @param offset  write into the buffer from this offset 
297:             * @param timestamp  timestamp value
298:             * @return DateTime.timestampRepresentationLength. This is the fixed 
299:             * length in bytes, taken to represent the timestamp value
300:             * @throws SqlException
301:             * @throws UnsupportedEncodingException
302:             */
303:            public static final int timestampToTimestampBytes(byte[] buffer,
304:                    int offset, java.sql.Timestamp timestamp)
305:                    throws SqlException, UnsupportedEncodingException {
306:                int year = timestamp.getYear() + 1900;
307:                if (year > 9999) {
308:                    throw new SqlException(null, new ClientMessageId(
309:                            SQLState.YEAR_EXCEEDS_MAXIMUM), new Integer(year),
310:                            "9999");
311:                }
312:                int month = timestamp.getMonth() + 1;
313:                int day = timestamp.getDate();
314:                int hour = timestamp.getHours();
315:                int minute = timestamp.getMinutes();
316:                int second = timestamp.getSeconds();
317:                int microsecond = timestamp.getNanos() / 1000;
318:
319:                char[] timestampChars = new char[DateTime.timestampRepresentationLength];
320:                int zeroBase = (int) '0';
321:                timestampChars[0] = (char) (year / 1000 + zeroBase);
322:                timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase);
323:                timestampChars[2] = (char) ((year % 100) / 10 + zeroBase);
324:                timestampChars[3] = (char) (year % 10 + +zeroBase);
325:                timestampChars[4] = '-';
326:                timestampChars[5] = (char) (month / 10 + zeroBase);
327:                timestampChars[6] = (char) (month % 10 + zeroBase);
328:                timestampChars[7] = '-';
329:                timestampChars[8] = (char) (day / 10 + zeroBase);
330:                timestampChars[9] = (char) (day % 10 + zeroBase);
331:                timestampChars[10] = '-';
332:                timestampChars[11] = (char) (hour / 10 + zeroBase);
333:                timestampChars[12] = (char) (hour % 10 + zeroBase);
334:                timestampChars[13] = '.';
335:                timestampChars[14] = (char) (minute / 10 + zeroBase);
336:                timestampChars[15] = (char) (minute % 10 + zeroBase);
337:                timestampChars[16] = '.';
338:                timestampChars[17] = (char) (second / 10 + zeroBase);
339:                timestampChars[18] = (char) (second % 10 + zeroBase);
340:                timestampChars[19] = '.';
341:                timestampChars[20] = (char) (microsecond / 100000 + zeroBase);
342:                timestampChars[21] = (char) ((microsecond % 100000) / 10000 + zeroBase);
343:                timestampChars[22] = (char) ((microsecond % 10000) / 1000 + zeroBase);
344:                timestampChars[23] = (char) ((microsecond % 1000) / 100 + zeroBase);
345:                timestampChars[24] = (char) ((microsecond % 100) / 10 + zeroBase);
346:                timestampChars[25] = (char) (microsecond % 10 + zeroBase);
347:
348:                // Network server expects to read the timestamp parameter value bytes with
349:                // UTF-8 encoding.  Reference - DERBY-1127
350:                // see DRDAConnThread.readAndSetParams
351:                byte[] timestampBytes = (new String(timestampChars))
352:                        .getBytes(Typdef.UTF8ENCODING);
353:                System.arraycopy(timestampBytes, 0, buffer, offset,
354:                        DateTime.timestampRepresentationLength);
355:
356:                return DateTime.timestampRepresentationLength;
357:            }
358:
359:            // *********************************************************
360:            // ******* CROSS output converters (byte[] -> class) *******
361:            // *********************************************************
362:
363:            /**
364:             * Expected character representation is DERBY string representation of a date
365:             * which is in JIS format: <code> yyyy-mm-dd </code>
366:             * 
367:             * @param buffer
368:             * @param offset
369:             * @param recyclableTimestamp
370:             * @param encoding                encoding of buffer
371:             * @return Timestamp translated from buffer with specified encoding
372:             * @throws UnsupportedEncodingException
373:             */
374:            public static final java.sql.Timestamp dateBytesToTimestamp(
375:                    byte[] buffer, int offset,
376:                    java.sql.Timestamp recyclableTimestamp, String encoding)
377:                    throws UnsupportedEncodingException {
378:                int year, month, day;
379:
380:                String date = new String(buffer, offset,
381:                        DateTime.dateRepresentationLength, encoding);
382:                int yearIndx, monthIndx, dayIndx;
383:
384:                yearIndx = 0;
385:                monthIndx = 5;
386:                dayIndx = 8;
387:
388:                int zeroBase = ((int) '0');
389:                // Character arithmetic is used rather than
390:                // the less efficient Integer.parseInt (date.substring()).
391:                year = 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + 100
392:                        * (((int) date.charAt(yearIndx + 1)) - zeroBase) + 10
393:                        * (((int) date.charAt(yearIndx + 2)) - zeroBase)
394:                        + (((int) date.charAt(yearIndx + 3)) - zeroBase) - 1900;
395:                month = 10 * (((int) date.charAt(monthIndx)) - zeroBase)
396:                        + (((int) date.charAt(monthIndx + 1)) - zeroBase) - 1;
397:                day = 10 * (((int) date.charAt(dayIndx)) - zeroBase)
398:                        + (((int) date.charAt(dayIndx + 1)) - zeroBase);
399:
400:                if (recyclableTimestamp == null) {
401:                    return new java.sql.Timestamp(year, month, day, 0, 0, 0, 0);
402:                } else {
403:                    recyclableTimestamp.setYear(year);
404:                    recyclableTimestamp.setMonth(month);
405:                    recyclableTimestamp.setDate(day);
406:                    recyclableTimestamp.setHours(0);
407:                    recyclableTimestamp.setMinutes(0);
408:                    recyclableTimestamp.setSeconds(0);
409:                    recyclableTimestamp.setNanos(0);
410:                    return recyclableTimestamp;
411:                }
412:            }
413:
414:            /**
415:             *  Expected character representation is DERBY string representation of time
416:             * which is in the format: <code> hh.mm.ss </code>
417:             * 
418:             * @param buffer
419:             * @param offset
420:             * @param recyclableTimestamp
421:             * @param encoding                 encoding of buffer
422:             * @return Timestamp translated from buffer with specified encoding 
423:             * @throws UnsupportedEncodingException
424:             * 
425:             */
426:            public static final java.sql.Timestamp timeBytesToTimestamp(
427:                    byte[] buffer, int offset,
428:                    java.sql.Timestamp recyclableTimestamp, String encoding)
429:                    throws UnsupportedEncodingException {
430:                int hour, minute, second;
431:
432:                String time = new String(buffer, offset,
433:                        DateTime.timeRepresentationLength, encoding);
434:                int zeroBase = ((int) '0');
435:
436:                // compute hour.
437:                hour = 10 * (((int) time.charAt(0)) - zeroBase)
438:                        + (((int) time.charAt(1)) - zeroBase);
439:                // compute minute.
440:                minute = 10 * (((int) time.charAt(3)) - zeroBase)
441:                        + (((int) time.charAt(4)) - zeroBase);
442:                // compute second   JIS format: hh:mm:ss.
443:                second = 10 * (((int) time.charAt(6)) - zeroBase)
444:                        + (((int) time.charAt(7)) - zeroBase);
445:
446:                if (recyclableTimestamp == null) {
447:                    return new java.sql.Timestamp(0, 0, 1, hour, minute,
448:                            second, 0);
449:                } else {
450:                    recyclableTimestamp.setYear(0);
451:                    recyclableTimestamp.setMonth(0);
452:                    recyclableTimestamp.setDate(1);
453:                    recyclableTimestamp.setHours(hour);
454:                    recyclableTimestamp.setMinutes(minute);
455:                    recyclableTimestamp.setSeconds(second);
456:                    recyclableTimestamp.setNanos(0);
457:                    return recyclableTimestamp;
458:                }
459:            }
460:
461:            /**
462:             * Expected character representation is DERBY string representation of a timestamp:
463:             * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
464:             * 
465:             * @param buffer
466:             * @param offset
467:             * @param recyclableDate
468:             * @param encoding             encoding of buffer
469:             * @return Date translated from buffer with specified encoding
470:             * @throws UnsupportedEncodingException
471:             */
472:            public static final java.sql.Date timestampBytesToDate(
473:                    byte[] buffer, int offset, java.sql.Date recyclableDate,
474:                    String encoding) throws UnsupportedEncodingException {
475:                int year, month, day;
476:
477:                String timestamp = new String(buffer, offset,
478:                        DateTime.timestampRepresentationLength, encoding);
479:                int zeroBase = ((int) '0');
480:
481:                year = 1000 * (((int) timestamp.charAt(0)) - zeroBase) + 100
482:                        * (((int) timestamp.charAt(1)) - zeroBase) + 10
483:                        * (((int) timestamp.charAt(2)) - zeroBase)
484:                        + (((int) timestamp.charAt(3)) - zeroBase) - 1900;
485:                month = 10 * (((int) timestamp.charAt(5)) - zeroBase)
486:                        + (((int) timestamp.charAt(6)) - zeroBase) - 1;
487:                day = 10 * (((int) timestamp.charAt(8)) - zeroBase)
488:                        + (((int) timestamp.charAt(9)) - zeroBase);
489:
490:                if (recyclableDate == null) {
491:                    return new java.sql.Date(year, month, day);
492:                } else {
493:                    recyclableDate.setYear(year);
494:                    recyclableDate.setMonth(month);
495:                    recyclableDate.setDate(day);
496:                    return recyclableDate;
497:                }
498:            }
499:
500:            /**
501:             * Expected character representation is DERBY string representation of a timestamp:
502:             * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
503:             * 
504:             * @param buffer
505:             * @param offset
506:             * @param recyclableTime
507:             * @param encoding            encoding of buffer
508:             * @return  Time translated from buffer with specified Encoding
509:             * @throws UnsupportedEncodingException
510:             */
511:            public static final java.sql.Time timestampBytesToTime(
512:                    byte[] buffer, int offset, java.sql.Time recyclableTime,
513:                    String encoding) throws UnsupportedEncodingException {
514:                int hour, minute, second;
515:
516:                String timestamp = new String(buffer, offset,
517:                        DateTime.timestampRepresentationLength, encoding);
518:                int zeroBase = ((int) '0');
519:
520:                hour = 10 * (((int) timestamp.charAt(11)) - zeroBase)
521:                        + (((int) timestamp.charAt(12)) - zeroBase);
522:                minute = 10 * (((int) timestamp.charAt(14)) - zeroBase)
523:                        + (((int) timestamp.charAt(15)) - zeroBase);
524:                second = 10 * (((int) timestamp.charAt(17)) - zeroBase)
525:                        + (((int) timestamp.charAt(18)) - zeroBase);
526:
527:                if (recyclableTime == null) {
528:                    return new java.sql.Time(hour, minute, second);
529:                } else {
530:                    recyclableTime.setYear(hour);
531:                    recyclableTime.setMonth(minute);
532:                    recyclableTime.setDate(second);
533:                    return recyclableTime;
534:                }
535:            }
536:
537:            // *********************************************************
538:            // ******* CROSS input converters (class -> byte[]) ********
539:            // *********************************************************
540:
541:            /**
542:             * java.sql.Timestamp is converted to character representation that is in JDBC date escape 
543:             * format: <code>yyyy-mm-dd</code>, which is the same as JIS date format in DERBY string representation of a date.
544:             * and then converted to bytes using UTF8 encoding.
545:             * @param buffer  
546:             * @param offset  write into the buffer from this offset 
547:             * @param timestamp  timestamp value
548:             * @return DateTime.dateRepresentationLength. This is the fixed length 
549:             * in bytes, that is taken to represent the timestamp value as a date.
550:             * @throws SqlException
551:             * @throws UnsupportedEncodingException
552:             */
553:            public static final int timestampToDateBytes(byte[] buffer,
554:                    int offset, java.sql.Timestamp timestamp)
555:                    throws SqlException, UnsupportedEncodingException {
556:                int year = timestamp.getYear() + 1900;
557:                if (year > 9999) {
558:                    throw new SqlException(null, new ClientMessageId(
559:                            SQLState.YEAR_EXCEEDS_MAXIMUM), new Integer(year),
560:                            "9999");
561:                }
562:                int month = timestamp.getMonth() + 1;
563:                int day = timestamp.getDate();
564:
565:                char[] dateChars = new char[DateTime.dateRepresentationLength];
566:                int zeroBase = (int) '0';
567:                dateChars[0] = (char) (year / 1000 + zeroBase);
568:                dateChars[1] = (char) ((year % 1000) / 100 + zeroBase);
569:                dateChars[2] = (char) ((year % 100) / 10 + zeroBase);
570:                dateChars[3] = (char) (year % 10 + +zeroBase);
571:                dateChars[4] = '-';
572:                dateChars[5] = (char) (month / 10 + zeroBase);
573:                dateChars[6] = (char) (month % 10 + zeroBase);
574:                dateChars[7] = '-';
575:                dateChars[8] = (char) (day / 10 + zeroBase);
576:                dateChars[9] = (char) (day % 10 + zeroBase);
577:                // Network server expects to read the date parameter value bytes with
578:                // UTF-8 encoding.  Reference - DERBY-1127
579:                // see DRDAConnThread.readAndSetParams
580:                byte[] dateBytes = (new String(dateChars))
581:                        .getBytes(Typdef.UTF8ENCODING);
582:                System.arraycopy(dateBytes, 0, buffer, offset,
583:                        DateTime.dateRepresentationLength);
584:
585:                return DateTime.dateRepresentationLength;
586:            }
587:
588:            /**
589:             * java.sql.Timestamp is converted to character representation in JDBC time escape format:
590:             *  <code>hh:mm:ss</code>, which is the same as
591:             * JIS time format in DERBY string representation of a time. The char representation is 
592:             * then converted to bytes using UTF8 encoding and written out into the buffer
593:             * @param buffer
594:             * @param offset  write into the buffer from this offset 
595:             * @param timestamp timestamp value
596:             * @return DateTime.timeRepresentationLength. This is the fixed length 
597:             * in bytes taken to represent the timestamp value as Time.
598:             * @throws UnsupportedEncodingException
599:             */
600:            public static final int timestampToTimeBytes(byte[] buffer,
601:                    int offset, java.sql.Timestamp timestamp)
602:                    throws UnsupportedEncodingException {
603:                int hour = timestamp.getHours();
604:                int minute = timestamp.getMinutes();
605:                int second = timestamp.getSeconds();
606:
607:                char[] timeChars = new char[DateTime.timeRepresentationLength];
608:                int zeroBase = (int) '0';
609:                timeChars[0] = (char) (hour / 10 + zeroBase);
610:                timeChars[1] = (char) (hour % 10 + +zeroBase);
611:                timeChars[2] = ':';
612:                timeChars[3] = (char) (minute / 10 + zeroBase);
613:                timeChars[4] = (char) (minute % 10 + zeroBase);
614:                timeChars[5] = ':';
615:                timeChars[6] = (char) (second / 10 + zeroBase);
616:                timeChars[7] = (char) (second % 10 + zeroBase);
617:
618:                // Network server expects to read the time parameter value bytes with
619:                // UTF-8 encoding.  Reference - DERBY-1127
620:                // see DRDAConnThread.readAndSetParams 
621:                byte[] timeBytes = (new String(timeChars))
622:                        .getBytes(Typdef.UTF8ENCODING);
623:                System.arraycopy(timeBytes, 0, buffer, offset,
624:                        DateTime.timeRepresentationLength);
625:
626:                return DateTime.timeRepresentationLength;
627:            }
628:
629:            /**
630:             * java.sql.Date is converted to character representation that is in DERBY string 
631:             * representation of a timestamp:<code>yyyy-mm-dd-hh.mm.ss.ffffff</code> and then 
632:             * converted to bytes using UTF8 encoding and written out to the buffer
633:             * @param buffer
634:             * @param offset offset in buffer to start writing to
635:             * @param date date value
636:             * @return DateTime.timestampRepresentationLength. This is the fixed length
637:             * in bytes, taken to represent the timestamp value.
638:             * @throws SqlException
639:             * @throws UnsupportedEncodingException
640:             */
641:            public static final int dateToTimestampBytes(byte[] buffer,
642:                    int offset, java.sql.Date date) throws SqlException,
643:                    UnsupportedEncodingException {
644:                int year = date.getYear() + 1900;
645:                if (year > 9999) {
646:                    throw new SqlException(null, new ClientMessageId(
647:                            SQLState.YEAR_EXCEEDS_MAXIMUM), new Integer(year),
648:                            "9999");
649:                }
650:                int month = date.getMonth() + 1;
651:                int day = date.getDate();
652:
653:                char[] timestampChars = new char[DateTime.timestampRepresentationLength];
654:                int zeroBase = (int) '0';
655:                timestampChars[0] = (char) (year / 1000 + zeroBase);
656:                timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase);
657:                timestampChars[2] = (char) ((year % 100) / 10 + zeroBase);
658:                timestampChars[3] = (char) (year % 10 + +zeroBase);
659:                timestampChars[4] = '-';
660:                timestampChars[5] = (char) (month / 10 + zeroBase);
661:                timestampChars[6] = (char) (month % 10 + zeroBase);
662:                timestampChars[7] = '-';
663:                timestampChars[8] = (char) (day / 10 + zeroBase);
664:                timestampChars[9] = (char) (day % 10 + zeroBase);
665:                timestampChars[10] = '-';
666:                timestampChars[11] = '0';
667:                timestampChars[12] = '0';
668:                timestampChars[13] = '.';
669:                timestampChars[14] = '0';
670:                timestampChars[15] = '0';
671:                timestampChars[16] = '.';
672:                timestampChars[17] = '0';
673:                timestampChars[18] = '0';
674:                timestampChars[19] = '.';
675:                timestampChars[20] = '0';
676:                timestampChars[21] = '0';
677:                timestampChars[22] = '0';
678:                timestampChars[23] = '0';
679:                timestampChars[24] = '0';
680:                timestampChars[25] = '0';
681:
682:                // Network server expects to read the timestamp parameter value bytes with
683:                // UTF-8 encoding.  Reference - DERBY-1127
684:                // see DRDAConnThread.readAndSetParams 
685:                byte[] timestampBytes = (new String(timestampChars))
686:                        .getBytes(Typdef.UTF8ENCODING);
687:                System.arraycopy(timestampBytes, 0, buffer, offset,
688:                        DateTime.timestampRepresentationLength);
689:
690:                return DateTime.timestampRepresentationLength;
691:            }
692:
693:            /**
694:             * java.sql.Time is converted to a character representation that is in DERBY string representation of a timestamp:
695:             * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code> and converted to bytes using UTF8 encoding 
696:             * @param buffer
697:             * @param offset offset in buffer to start writing to
698:             * @param time time value
699:             * @return DateTime.timestampRepresentationLength which is the fixed length
700:             * taken up by the conversion of time to timestamp in bytes
701:             * @throws UnsupportedEncodingException
702:             */
703:            public static final int timeToTimestampBytes(byte[] buffer,
704:                    int offset, java.sql.Time time)
705:                    throws UnsupportedEncodingException {
706:                int hour = time.getHours();
707:                int minute = time.getMinutes();
708:                int second = time.getSeconds();
709:
710:                char[] timestampChars = new char[DateTime.timestampRepresentationLength];
711:                int zeroBase = (int) '0';
712:                timestampChars[0] = '1';
713:                timestampChars[1] = '9';
714:                timestampChars[2] = '0';
715:                timestampChars[3] = '0';
716:                timestampChars[4] = '-';
717:                timestampChars[5] = '0';
718:                timestampChars[6] = '1';
719:                timestampChars[7] = '-';
720:                timestampChars[8] = '0';
721:                timestampChars[9] = '1';
722:                timestampChars[10] = '-';
723:                timestampChars[11] = (char) (hour / 10 + zeroBase);
724:                timestampChars[12] = (char) (hour % 10 + zeroBase);
725:                timestampChars[13] = '.';
726:                timestampChars[14] = (char) (minute / 10 + zeroBase);
727:                timestampChars[15] = (char) (minute % 10 + zeroBase);
728:                timestampChars[16] = '.';
729:                timestampChars[17] = (char) (second / 10 + zeroBase);
730:                timestampChars[18] = (char) (second % 10 + zeroBase);
731:                timestampChars[19] = '.';
732:                timestampChars[20] = '0';
733:                timestampChars[21] = '0';
734:                timestampChars[22] = '0';
735:                timestampChars[23] = '0';
736:                timestampChars[24] = '0';
737:                timestampChars[25] = '0';
738:
739:                // Network server expects to read the timestamp parameter value bytes with
740:                // UTF-8 encoding.  Reference - DERBY-1127
741:                // see DRDAConnThread.readAndSetParams for TIMESTAMP
742:                byte[] timestampBytes = (new String(timestampChars))
743:                        .getBytes(Typdef.UTF8ENCODING);
744:                System.arraycopy(timestampBytes, 0, buffer, offset,
745:                        DateTime.timestampRepresentationLength);
746:
747:                return DateTime.timestampRepresentationLength;
748:            }
749:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.