KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > converters > DateConverter


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.converters;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.text.DateFormat JavaDoc;
20 import java.text.ParseException JavaDoc;
21 import java.util.Calendar JavaDoc;
22
23 import org.apache.commons.beanutils.Converter;
24
25 /**
26  * @author tierney.matt
27  */

28 public class DateConverter implements Converter {
29   private DateFormat JavaDoc dateFormat;
30
31   public DateConverter(DateFormat JavaDoc dateFormat) {
32     this.dateFormat = dateFormat;
33   }
34
35   public Object JavaDoc convert(Class JavaDoc destClass, Object JavaDoc sourceObj) {
36     Object JavaDoc result = null;
37
38     Class JavaDoc sourceFieldClass = sourceObj.getClass();
39     long time = -1;
40     //Calendar to Date
41
if (Calendar JavaDoc.class.isAssignableFrom(sourceFieldClass)) {
42       Calendar JavaDoc inVal = (Calendar JavaDoc) sourceObj;
43       time = inVal.getTime().getTime();
44     //Date to Date
45
} else if (java.util.Date JavaDoc.class.isAssignableFrom(sourceFieldClass)) {
46         time = ( (java.util.Date JavaDoc) sourceObj).getTime();
47     //String to Date
48
} else if (dateFormat != null && String JavaDoc.class.isAssignableFrom(sourceObj.getClass())) {
49         try {
50           if("".equals(sourceObj)){
51             return null;
52           }
53           time = dateFormat.parse( (String JavaDoc) sourceObj).getTime();
54         } catch (ParseException JavaDoc e) {
55           throw new ConversionException("Unable to parse source object using specified date format", e);
56         }
57     //Default conversion
58
} else {
59       try {
60         time = Long.parseLong(sourceObj.toString());
61       } catch (NumberFormatException JavaDoc e) {
62         throw new ConversionException("Unable to determine time in millis of source object",e);
63       }
64     }
65
66     try {
67       Constructor JavaDoc constructor = destClass.getConstructor(new Class JavaDoc[] {Long.TYPE});
68       result = constructor.newInstance(new Object JavaDoc[] {new Long JavaDoc(time)});
69     } catch (Exception JavaDoc e) {
70       throw new ConversionException(e);
71     }
72
73     return result;
74
75   }
76
77   public DateFormat JavaDoc getDateFormat() {
78     return dateFormat;
79   }
80
81   public void setDateFormat(DateFormat JavaDoc dateFormat) {
82     this.dateFormat = dateFormat;
83   }
84 }
85
Popular Tags