|
Hibernate example source code file (JdbcDateTypeDescriptor.java)
The Hibernate JdbcDateTypeDescriptor.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.type.descriptor.java; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.hibernate.HibernateException; import org.hibernate.type.descriptor.WrapperOptions; /** * TODO : javadoc * * @author Steve Ebersole */ public class JdbcDateTypeDescriptor extends AbstractTypeDescriptor<Date> { public static final JdbcDateTypeDescriptor INSTANCE = new JdbcDateTypeDescriptor(); public static final String DATE_FORMAT = "dd MMMM yyyy"; public static class DateMutabilityPlan extends MutableMutabilityPlan<Date> { public static final DateMutabilityPlan INSTANCE = new DateMutabilityPlan(); public Date deepCopyNotNull(Date value) { return java.sql.Date.class.isInstance( value ) ? new java.sql.Date( value.getTime() ) : new Date( value.getTime() ); } } public JdbcDateTypeDescriptor() { super( Date.class, DateMutabilityPlan.INSTANCE ); } public String toString(Date value) { return new SimpleDateFormat( DATE_FORMAT ).format( value ); } public Date fromString(String string) { try { return new Date( new SimpleDateFormat(DATE_FORMAT).parse( string ).getTime() ); } catch ( ParseException pe) { throw new HibernateException( "could not parse date string" + string, pe ); } } @Override public boolean areEqual(Date one, Date another) { if ( one == another ) { return true; } if ( one == null || another == null ) { return false; } if ( one.getTime() == another.getTime() ) { return true; } Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.setTime( one ); calendar2.setTime( another ); return calendar1.get( Calendar.MONTH ) == calendar2.get( Calendar.MONTH ) && calendar1.get( Calendar.DAY_OF_MONTH ) == calendar2.get( Calendar.DAY_OF_MONTH ) && calendar1.get( Calendar.YEAR ) == calendar2.get( Calendar.YEAR ); } @Override public int extractHashCode(Date value) { Calendar calendar = Calendar.getInstance(); calendar.setTime( value ); int hashCode = 1; hashCode = 31 * hashCode + calendar.get( Calendar.MONTH ); hashCode = 31 * hashCode + calendar.get( Calendar.DAY_OF_MONTH ); hashCode = 31 * hashCode + calendar.get( Calendar.YEAR ); return hashCode; } @SuppressWarnings({ "unchecked" }) public <X> X unwrap(Date value, Class Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate JdbcDateTypeDescriptor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.