KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > oracle > OracleUtilDateType


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba.oracle;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.Types JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Date JavaDoc;
27
28 import org.apache.cayenne.access.types.UtilDateType;
29
30 /**
31  * @author Andrus Adamchik
32  */

33 public class OracleUtilDateType extends UtilDateType {
34
35     public Object JavaDoc materializeObject(CallableStatement JavaDoc cs, int index, int type)
36         throws Exception JavaDoc {
37             
38         Object JavaDoc date = super.materializeObject(cs, index, type);
39         if (date == null || type != Types.TIME) {
40             return date;
41         } else {
42             return normalizeDate((Date JavaDoc) date);
43         }
44     }
45
46     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type)
47         throws Exception JavaDoc {
48
49         Object JavaDoc date = super.materializeObject(rs, index, type);
50         if (date == null || type != Types.TIME) {
51             return date;
52         } else {
53             return normalizeDate((Date JavaDoc) date);
54         }
55     }
56
57     /**
58      * Offsets date component to be January 1, 1970,
59      * since Oracle adapter returns time based on January 1, 1900.
60      */

61     protected Date JavaDoc normalizeDate(Date JavaDoc time) {
62         // this may need serious optimization - Calendar is slow...
63
Calendar JavaDoc calendar = Calendar.getInstance();
64         calendar.setTime(time);
65         calendar.set(Calendar.YEAR, 1970);
66         calendar.set(Calendar.MONTH, 0);
67         calendar.set(Calendar.DAY_OF_MONTH, 1);
68
69         return calendar.getTime();
70     }
71 }
72
Popular Tags