001 package org.apache.ojb.broker.accesslayer.conversions;
002
003 /* Copyright 2004-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.sql.Timestamp;
019 import java.util.Calendar;
020 import java.util.GregorianCalendar;
021
022 /**
023 * This implementation of the {@link FieldConversion} interface converts
024 * between {@link java.util.Calendar} and {@link java.sql.Timestamp}. When
025 * convert sql to java always a {@link java.util.GregorianCalendar} object
026 * is returned.
027 *
028 * @author Guillaume Nodet
029 * @version $Id: Calendar2TimestampFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
030 */
031 public class Calendar2TimestampFieldConversion implements FieldConversion
032 {
033 /**
034 * @see FieldConversion#javaToSql(Object)
035 */
036 public Object javaToSql(Object source)
037 {
038 if (source instanceof Calendar)
039 {
040 // only valid >= JDK 1.4
041 // return new Date(((Calendar) source).getTimeInMillis());
042 return new Timestamp(((Calendar) source).getTime().getTime());
043 }
044 else
045 {
046 return source;
047 }
048 }
049
050 /**
051 * @see FieldConversion#sqlToJava(Object)
052 */
053 public Object sqlToJava(Object source)
054 {
055 if (source instanceof Timestamp)
056 {
057 GregorianCalendar cal = new GregorianCalendar();
058 // only valid >= JDK 1.4
059 // cal.setTimeInMillis(((Date) source).getTime());
060 cal.setTime(((Timestamp) source));
061 return cal;
062 }
063 else
064 {
065 return source;
066 }
067 }
068 }
069
070