1 package org.hibernate.type; 3 4 import java.sql.Date ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.sql.Types ; 9 import java.text.DateFormat ; 10 import java.text.ParseException ; 11 import java.text.SimpleDateFormat ; 12 import java.util.Calendar ; 13 14 import org.hibernate.EntityMode; 15 import org.hibernate.Hibernate; 16 import org.hibernate.HibernateException; 17 18 22 public class DateType extends MutableType implements IdentifierType, LiteralType { 23 24 private static final String DATE_FORMAT = "dd MMMM yyyy"; 25 26 public Object get(ResultSet rs, String name) throws SQLException { 27 return rs.getDate(name); 28 } 29 30 public Class getReturnedClass() { 31 return java.util.Date .class; 32 } 33 34 public void set(PreparedStatement st, Object value, int index) throws SQLException { 35 36 Date sqlDate; 37 if ( value instanceof Date ) { 38 sqlDate = (Date ) value; 39 } 40 else { 41 sqlDate = new Date ( ( (java.util.Date ) 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 x, Object y) { 51 52 if (x==y) return true; 53 if (x==null || y==null) return false; 54 55 java.util.Date xdate = (java.util.Date ) x; 56 java.util.Date ydate = (java.util.Date ) y; 57 58 if ( xdate.getTime()==ydate.getTime() ) return true; 59 60 Calendar calendar1 = java.util.Calendar.getInstance(); 61 Calendar 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 x, EntityMode entityMode) { 69 Calendar calendar = java.util.Calendar.getInstance(); 70 calendar.setTime( (java.util.Date ) x ); 71 return Hibernate.CALENDAR_DATE.getHashCode(calendar, entityMode); 72 } 73 74 public String getName() { return "date"; } 75 76 public String toString(Object val) { 77 return new SimpleDateFormat (DATE_FORMAT).format( (java.util.Date ) val ); 78 } 79 80 public Object deepCopyNotNull(Object value) { 81 return new Date ( ( (java.util.Date ) value ).getTime() ); 82 } 83 84 public Object stringToObject(String xml) throws Exception { 85 return DateFormat.getDateInstance().parse(xml); 86 } 87 88 public String objectToSQLString(Object value) throws Exception { 89 return '\'' + value.toString() + '\''; 90 } 91 92 public Object fromStringValue(String xml) throws HibernateException { 93 try { 94 return new SimpleDateFormat (DATE_FORMAT).parse(xml); 95 } 96 catch (ParseException pe) { 97 throw new HibernateException("could not parse XML", pe); 98 } 99 } 100 101 } 102 103 104 105 106 107 | Popular Tags |