KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > conversions > Calendar2TimestampFieldConversion


1 package org.apache.ojb.broker.accesslayer.conversions;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.GregorianCalendar JavaDoc;
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 FieldConversion
32 {
33     /**
34      * @see FieldConversion#javaToSql(Object)
35      */

36     public Object JavaDoc javaToSql(Object JavaDoc source)
37     {
38         if (source instanceof Calendar JavaDoc)
39         {
40             // only valid >= JDK 1.4
41
// return new Date(((Calendar) source).getTimeInMillis());
42
return new Timestamp JavaDoc(((Calendar JavaDoc) source).getTime().getTime());
43         }
44         else
45         {
46             return source;
47         }
48     }
49
50     /**
51      * @see FieldConversion#sqlToJava(Object)
52      */

53     public Object JavaDoc sqlToJava(Object JavaDoc source)
54     {
55         if (source instanceof Timestamp JavaDoc)
56         {
57             GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
58             // only valid >= JDK 1.4
59
// cal.setTimeInMillis(((Date) source).getTime());
60
cal.setTime(((Timestamp JavaDoc) source));
61             return cal;
62         }
63         else
64         {
65             return source;
66         }
67     }
68 }
69
70
71
Popular Tags