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.Date;
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.Date}. When
25: * convert sql to java always a {@link java.util.GregorianCalendar}
26: * object is returned.
27: *
28: * @author Guillaume Nodet
29: * @version $Id: Calendar2DateFieldConversion.java,v 1.2.2.1 2005/12/21 22:23:38 tomdz Exp $
30: */
31: public class Calendar2DateFieldConversion implements FieldConversion {
32: /**
33: * @see FieldConversion#javaToSql(Object)
34: */
35: public Object javaToSql(Object source) {
36: if (source instanceof Calendar) {
37: // only valid >= JDK 1.4
38: // return new Date(((Calendar) source).getTimeInMillis());
39: return new Date(((Calendar) source).getTime().getTime());
40: } else {
41: return source;
42: }
43: }
44:
45: /**
46: * @see FieldConversion#sqlToJava(Object)
47: */
48: public Object sqlToJava(Object source) {
49: if (source instanceof Date) {
50: GregorianCalendar cal = new GregorianCalendar();
51: cal.setTime(((Date) source));
52: return cal;
53: } else {
54: return source;
55: }
56: }
57: }
|