KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > DateType


1 //$Id: DateType.java,v 1.6 2005/06/22 03:52:32 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.Date JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Types JavaDoc;
9 import java.text.DateFormat JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import java.text.SimpleDateFormat JavaDoc;
12 import java.util.Calendar JavaDoc;
13
14 import org.hibernate.EntityMode;
15 import org.hibernate.Hibernate;
16 import org.hibernate.HibernateException;
17
18 /**
19  * <tt>date</tt>: A type that maps an SQL DATE to a Java Date.
20  * @author Gavin King
21  */

22 public class DateType extends MutableType implements IdentifierType, LiteralType {
23
24     private static final String JavaDoc DATE_FORMAT = "dd MMMM yyyy";
25
26     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
27         return rs.getDate(name);
28     }
29
30     public Class JavaDoc getReturnedClass() {
31         return java.util.Date JavaDoc.class;
32     }
33
34     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws SQLException JavaDoc {
35
36         Date JavaDoc sqlDate;
37         if ( value instanceof Date JavaDoc) {
38             sqlDate = (Date JavaDoc) value;
39         }
40         else {
41             sqlDate = new Date JavaDoc( ( (java.util.Date JavaDoc) value ).getTime() );
42         }
43         st.setDate(index, sqlDate);
44     }
45
46     public int sqlType() {
47         return Types.DATE;
48     }
49
50     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
51
52         if (x==y) return true;
53         if (x==null || y==null) return false;
54
55         java.util.Date JavaDoc xdate = (java.util.Date JavaDoc) x;
56         java.util.Date JavaDoc ydate = (java.util.Date JavaDoc) y;
57         
58         if ( xdate.getTime()==ydate.getTime() ) return true;
59         
60         Calendar JavaDoc calendar1 = java.util.Calendar.getInstance();
61         Calendar JavaDoc calendar2 = java.util.Calendar.getInstance();
62         calendar1.setTime( xdate );
63         calendar2.setTime( ydate );
64
65         return Hibernate.CALENDAR_DATE.isEqual(calendar1, calendar2);
66     }
67
68     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
69         Calendar JavaDoc calendar = java.util.Calendar.getInstance();
70         calendar.setTime( (java.util.Date JavaDoc) x );
71         return Hibernate.CALENDAR_DATE.getHashCode(calendar, entityMode);
72     }
73     
74     public String JavaDoc getName() { return "date"; }
75
76     public String JavaDoc toString(Object JavaDoc val) {
77         return new SimpleDateFormat JavaDoc(DATE_FORMAT).format( (java.util.Date JavaDoc) val );
78     }
79
80     public Object JavaDoc deepCopyNotNull(Object JavaDoc value) {
81         return new Date JavaDoc( ( (java.util.Date JavaDoc) value ).getTime() );
82     }
83
84     public Object JavaDoc stringToObject(String JavaDoc xml) throws Exception JavaDoc {
85         return DateFormat.getDateInstance().parse(xml);
86     }
87
88     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
89         return '\'' + value.toString() + '\'';
90     }
91
92     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
93         try {
94             return new SimpleDateFormat JavaDoc(DATE_FORMAT).parse(xml);
95         }
96         catch (ParseException JavaDoc pe) {
97             throw new HibernateException("could not parse XML", pe);
98         }
99     }
100
101 }
102
103
104
105
106
107
Popular Tags