01: package org.apache.ojb.broker.accesslayer.conversions;
02:
03: /* Copyright 2004-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import java.sql.Timestamp;
19: import java.util.Calendar;
20: import java.util.GregorianCalendar;
21:
22: /**
23: * This implementation of the {@link FieldConversion} interface converts
24: * between {@link java.util.Calendar} and {@link java.sql.Timestamp}. When
25: * convert sql to java always a {@link java.util.GregorianCalendar} object
26: * is returned.
27: *
28: * @author Guillaume Nodet
29: * @version $Id: Calendar2TimestampFieldConversion.java,v 1.2.2.1 2005/12/21 22:23:38 tomdz Exp $
30: */
31: public class Calendar2TimestampFieldConversion implements
32: FieldConversion {
33: /**
34: * @see FieldConversion#javaToSql(Object)
35: */
36: public Object javaToSql(Object source) {
37: if (source instanceof Calendar) {
38: // only valid >= JDK 1.4
39: // return new Date(((Calendar) source).getTimeInMillis());
40: return new Timestamp(((Calendar) source).getTime()
41: .getTime());
42: } else {
43: return source;
44: }
45: }
46:
47: /**
48: * @see FieldConversion#sqlToJava(Object)
49: */
50: public Object sqlToJava(Object source) {
51: if (source instanceof Timestamp) {
52: GregorianCalendar cal = new GregorianCalendar();
53: // only valid >= JDK 1.4
54: // cal.setTimeInMillis(((Date) source).getTime());
55: cal.setTime(((Timestamp) source));
56: return cal;
57: } else {
58: return source;
59: }
60: }
61: }
|